androidstudio按钮点击变色
时间: 2023-07-29 22:09:37 浏览: 199
要实现 Android Studio 按钮点击变色的效果,可以通过以下步骤进行操作:
1. 在 res 文件夹下创建一个新的 drawable 资源文件,例如 button_bg.xml。
2. 在该文件中定义按钮点击变色的效果,例如:
```
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="@color/colorPrimaryDark" />
<item android:drawable="@color/colorPrimary" />
</selector>
```
这里使用了 selector 标签,表示根据按钮的状态来选择显示哪个 drawable。当按钮被按下时,显示 colorPrimaryDark 颜色;否则显示 colorPrimary 颜色。
3. 将该 drawable 应用到按钮的 background 属性中,例如:
```
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click Me"
android:background="@drawable/button_bg" />
```
这样,当用户点击按钮时,按钮的背景颜色就会变为 colorPrimaryDark。
希望这些步骤可以帮助到你实现 Android Studio 按钮点击变色的效果。
阅读全文
相关推荐
















