1.
首先建立 Selector(customize_checkbox_selector.xml),以套用到checkbox上
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/press"
android:state_checked="true" android:state_focused="true"/>
<item android:drawable="@drawable/press"
android:state_checked="false" android:state_focused="true"/>
<item android:drawable="@drawable/press" android:state_checked="true"/>
<item android:drawable="@drawable/normal" android:state_checked="false"/>
</selector>
注意:
press的圖片就是點擊checkbox顯示的圖片, normal的圖片就是未點擊checkbox的圖片
把customize_checkbox_selector.xml放到res/drawable/中
2.
建立Style, 和 Button 不同,checkbox必須另外建立一個style,並在style中引入第一步驟建立的Selector, 最後才在checkbox的xml中套用style
<style name="customize_checkbox_style"
parent="@android:style/Widget.CompoundButton.CheckBox">
<item name="android:button">@drawable/customize_checkbox_selector</item>
</style>
3.
在checkbox xml中套用
<CheckBox
android:id="@+id/test_checkbox"
style="@style/customize_checkbox_style"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/customize_bg"
android:text="@string/teststring">
</CheckBox>
注意: 若checkbox的文字已經有設定了但在畫面上還是看不出來,請小心checkbox的字體顏色是否和背景顏色相同