我的习惯是,资源一般放在drawable-hdpi,drawable-mdpi,drawable-ldpi这三个文件夹中,而对于定义selector的文件,则在res下新建一个drawable文件夹并放在里面。
该selector代码如下:
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/btn_green_selected" android:state_pressed="true"/> <item android:drawable="@drawable/btn_green_normal"/> </selector>
在以上的item的属性中,与状态相关的属性有如下12个,它们的值为true或false,定义的是在这些状态下显示的资源,并且它些属性也可以结合使用,即在一个item中定义多个state的值。
android:state_active="" android:state_checkable="" android:state_checked="" android:state_first="" android:state_enabled="" android:state_focused=" " android:state_last="" android:state_middle="" android:state_pressed="" android:state_selected="" android:state_single="true" android:state_window_focused=""
效果如下,看那个“记录”的按钮:
未按下效果:
按下效果: