1. 程式人生 > >Android中自定義checkbox樣式

Android中自定義checkbox樣式

1.首先在drawable資料夾中新增drawable檔案checkbox_style.xml。

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@drawable/checkbox_pressed" android:state_checked="true"/>
    <item android:drawable="@drawable/checkbox_normal" android:state_checked="false"/>
    <item android:drawable="@drawable/checkbox_normal"/>

</selector>
2.在values資料夾下的styles.xml檔案中新增CustomCheckboxTheme樣式。
<style name="CustomCheckboxTheme" parent="@android:style/Widget.CompoundButton.CheckBox">
	<item name="android:button">@drawable/checkbox_style</item>
</style>
3.在佈局檔案中使用CustomCheckboxTheme樣式。
<CheckBox
        android:id="@+id/select_all"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        style="@style/CustomCheckboxTheme" />

使用到的圖片資源

checkbox_normal.png

checkbox_pressed.png