创建selector_checkbox.xml文件内容如下
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true" android:drawable="@drawable/check_on"/>
<item android:state_checked="true" android:drawable="@drawable/check_on"/>
<item android:state_pressed="true" android:drawable="@drawable/check_on"/>
<item android:drawable="@drawable/check_off"/>
</selector>
在checkbox控件中调用selector_checkbox.xml文件
<CheckBox
android:id="@+id/e_userstate"
android:layout_toRightOf="@+id/t_userstate"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_marginLeft="15dp"
android:layout_centerVertical="true"
android:background="@drawable/selector_checkbox"
android:button="@null"
/>
<TextView
android:id="@+id/tip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_centerVertical="true"
android:text="@string/user_enable"
android:layout_toRightOf="@+id/e_userstate"
/>
注意:必须将android:button设置为@null。
后来发现可以直接设置android:button的背景也可以达到效果
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:button="@drawable/selector_checkbox"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
android:text="自动登录"
/>