参考:
http://endual.iteye.com/blog/1534258
总结:
定义res/drawable/button_style.xml文件
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="@drawable/play_press" />
<item android:state_focused="true" android:drawable="@drawable/play_press" />
<item android:drawable="@drawable/play" />
</selector>
以上需要放两种图片,play_press.png和play.png
也可以直接使用颜色:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="@color/chocolate" />
<item android:state_focused="true" android:drawable="@color/darkslategray" />
<item android:drawable="@color/tan" />
</selector>
使用style文件
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/button_style" ></Button> </LinearLayout>