1、设置CheckBox选中/不选中,按下/不按下状态的样式
<1>、在drawable中配置checkbox.xml
<?xml version="1.0" encoding="UTF-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_checked="true" android:state_pressed="true" android:drawable="@drawable/focused" ;/> <item android:state_checked="false" android:state_pressed="true" android:drawable="@drawable/normal" ;/> <item android:state_checked="false" android:drawable="@drawable/normal" ;/> <item android:state_checked="true" android:drawable="@drawable/focused" ;/> </selector>
<2>、在res/values/styles.xml文件中添加MyCheckBox名称的Style
<style name="MyCheckBox"> <item name="android:button">@drawable/checkbox</item> </style>
<3>、在布局文件中设置CheckBox的style属性,如:
<CheckBox android:layout_width="wrap_content" android:layout_height="wrap_content" style="@style/MyCheckBox"/>
2、当CheckBox作为ListView中ListItem的子组件时,ChekBox要有下面三个配置
android:focusable="false"
因为checkbox的点击事件优先级比listview的高,所以要在checkbox中添加android:focusable="false",使得checkbox初始的时候没有获取焦点。