1.自定義樣式+擴大點擊區域CheckBox
<CheckBox
android:id="@+id/cb_select_item"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:button="@null"
android:drawableLeft="@drawable/checkbox_button_iamge"
android:drawableStart="@drawable/checkbox_button_iamge"
android:padding="@dimen/common_measure_30dp"
android:focusable="false"
android:focusableInTouchMode="false"
android:clickable="false"
/>
2.checkbox_button_iamge 文件是選擇器
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@mipmap/sel_image2" android:state_checked="true" /> <item android:drawable="@mipmap/sel_image2" android:state_selected="true" /> <item android:drawable="@mipmap/sel_image2" android:state_pressed="true" /> <item android:drawable="@mipmap/unsel_image2" android:state_checked="false" /> </selector>
由於CheckBox的點擊事件優先級比ListView/GridView 的高,所以要在checkbox中添加android:focusable="false" android:focusableInTouchMode="false" android:clickable="false",使得CheckBox初始的時候沒有獲取焦點。