在res/values/colors.xml文件中为各种状态定义不同的颜色如下(请使用您自己的十六进制代码作为颜色)
<color name="green_pressed">#ff00f000</color>
<color name="green_focused">#ff00f700</color>
<color name="green_default">#ff00ff00</color>
为不同的状态声明各种可绘制对象
button_focused_green.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<solid android:color="@color/green_focused" />
<!-- optional, remove if you don't want round border -->
<corners android:radius="4dp" />
</shape>
button_pressed_green.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<solid android:color="@color/green_pressed" />
<!-- optional, remove if you don't want round border -->
<corners android:radius="4dp" />
</shape>
button_default_green.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<solid android:color="@color/green_default" />
<!-- optional, remove if you don't want round border -->
<corners android:radius="4dp" />
</shape>
声明一个StateListDrawable
in xml 文件,该文件将应用于Button
button_green.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/button_focused_green" android:state_focused="true"/>
<item android:drawable="@drawable/button_pressed_green" android:state_pressed="true"/>
<item android:drawable="@drawable/button_default_green"/>
</selector>
Button
在布局 xml 中设置 background 属性
<Button
...
android:background="@drawable/button_green"
.../>
希望这可以帮助。