android button选中颜色改变,android Button 颜色的变化(点击,放开,点击不放)
时间: 2023-12-10 20:39:04 浏览: 104
要改变 Android Button 的选中颜色,可以使用 StateListDrawable。StateListDrawable是一个可绘制的对象,它可以根据 Button 的状态(按下,选中等)来显示不同的图片或颜色。
以下是一些示例代码:
1. 定义一个名为 button_bg.xml 的 StateListDrawable 文件,用于定义不同状态下的颜色:
```
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:color="#FF4081"/> <!-- pressed -->
<item android:state_focused="true" android:color="#4A148C"/> <!-- focused -->
<item android:color="#9C27B0"/> <!-- default -->
</selector>
```
2. 在 Button 的 XML 文件中应用这个背景:
```
<Button
android:id="@+id/myButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click me"
android:background="@drawable/button_bg"/>
```
这样,当 Button 被选中时,它的背景颜色将会根据 button_bg.xml 中定义的状态进行变化。
阅读全文
相关推荐
















