最新做项目需求自定义CheckBox 的样式,总结了以下几个步骤:
1.首先在drawable文件夹中添加drawable文件
my_checkbox
.xml
<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="custom_checkbox" parent="@android:style/Widget.CompoundButton.CheckBox">
<item name="android:button">@drawable/my_checkbox</item>
</style>
3.在CheckBox中使用CustomCheckboxTheme样式
<CheckBox
android:id="@+id/checked"
style="@style/custom_checkbox"
android:layout_width="40dip"
android:layout_height="wrap_content"
android:layout_marginLeft="10dip"
android:checked="false" />