Android中自定義checkbox樣式
阿新 • • 發佈:2019-01-24
1.首先在drawable資料夾中新增drawable檔案checkbox_style.xml。
2.在values資料夾下的styles.xml檔案中新增CustomCheckboxTheme樣式。<?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>
<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