有些时候需要根据项目需求自定义CheckBox 的样式,步骤如下:
1.首先在res / drawable文件夹中添加drawable文件my_checkbox.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/checkbox_checked" android:state_checked="true"/><!-- 选中状态 -->
<item android:drawable="@drawable/checkbox_default" android:state_checked="false"/><!-- 未选中 -->
<item android:drawable="@drawable/checkbox_default"/><!-- 默认正常状态 -->
</selector>
2.在CheckBox中使用my_checkbox
<CheckBox
android:id="@+id/checkBox"
style="@android:style/Widget.CompoundButton.CheckBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:button="@drawable/my_checkbox"
android:text="平安出行保险 普通型" />
未选中状态 的图片 |
选中状态 的图片 |