自定义checkbox风格

1.在drawable中创建文件checkbox_selector.xml:

 <?xml version="1.0" encoding="utf-8"?>

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_checked="true" 
          android:drawable="@drawable/checkbox_ok" /><!--设置选中图片-->
    <item android:state_checked="false" 
         android:drawable="@drawable/checkbox_empty" /><!--设置未选中图片-->
</selector>

2. 在values中创建styles.xml:

 <?xml version="1.0" encoding="utf-8"?>


<resources>

      <style name="MyCheckBox" parent="@android:style/Widget.CompoundButton.CheckBox">

       <item name="android:button">@drawable/checkbox_selector</item>

       <item name="android:paddingLeft">25.0dip</item>

       <item name="android:maxHeight">10.0dip</item>

      </style>

</resources>

3. 在你的CheckBox中添加属性:

<CheckBox
        android:id="@+id/check"
        android:layout_width="20dp"
        android:layout_height="20dp"
        android:layout_marginLeft="5dp"
        style="@style/MyCheckBox"  
        />


搞定!这样就把你的checkbox换成你设置的那两张图片了

岑忠满的博客新站点 https://cenzm.xyz
原文地址:https://www.cnblogs.com/cenzhongman/p/6057538.html