监听view获得焦点或按下等状态的背景,可以用selector;
在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/btn_bg_fc" /><!--按下时的背景--> <item android:state_focused="true" android:drawable="@drawable/btn_bg_fc" /><!--获得焦点时的背景--> <item android:state_selected="true" android:drawable="@drawable/btn_bg_fc" /><!--选中状态的背景--> <item android:drawable="@drawable/btn_bg" /> <!--默认背景--> </selector>
用在Listview,gridview中
<ListView
...... android:listSelector="@drawable/button_style"
......> </ListView>
用于其他View,如Button,textview等
android:background="@drawable/button_style"属性