有些時候需要根據項目需求自定義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="平安出行保險 普通型" />
未選中狀態 的圖片 |
選中狀態 的圖片 |