使用自定义的可绘制选择器替换Android复选框样式

13
我尝试使用以下内容创建了一个XML选择器:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/shuffleon" android:state_checked="true" />
    <item android:drawable="@drawable/shuffleoff" android:state_selected="false" />
</selector>

当我尝试将backgroundDrawable设置为CheckBox时,复选框并没有替换CheckBox的风格:

  shuffle.setBackground(android.support.v4.content.res.ResourcesCompat.getDrawable(getResources(), R.drawable.shuffle, null));

在这个问题的基础上:如何更改Android CheckBox的选中和未选中图标,我需要通过XML设置button drawableandroid:button="@drawable/checkbox"但是我无法这样做,因为我正在以编程方式创建CheckBox


你尝试过使用yourcheckbox.setButtonDrawable() 吗? - Madhu
我不知道有这样的命令,请将其发布为答案,以帮助其他人。 - Marian Pavel
你试过我的代码了吗? - Sunil
7个回答

24

简单方法

  1. 在drawable文件夹中创建一个新布局并将其命名为custom_checkbox(您也可以重命名)

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/Checked_image_name"android:state_checked="true"/>
<item android:drawable="@drawable/Unchecked_image_name" android:state_checked="false"/>
</selector>
  • 在您的布局活动中使用此代码

  • <CheckBox
    android:id="@+id/checkbox"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:button="@drawable/custom_checkbox"/>
    

    可能不应该为这样的自定义设置<item name="checkboxStyle">@style/YourTheme.CheckBox</item> - undefined

    13

    使用以下代码行,我认为它会起作用

    yourcheckbox.setButtonDrawable(yourdrawable); 
    

    在我的情况下,它与 android:button 是一样的。 - undefined

    5
    如果您使用sdk 28 (androidx)及以上版本,请尝试使用以下内容:
    androidx.appcompat.widget.AppCompatCheckBox
    

    替代

    Checkbox
    

    5
    使用这个
    shuffle.setButtonDrawable(R.drawable.custom_checkbox_selector);
    

    XML代码;

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
    
        <item android:drawable="@drawable/radiobutton_checked" android:state_checked="true"/>
        <item android:drawable="@drawable/radiobutton_unchecked" android:state_checked="false"/>
    
    </selector>
    

    4
    这对我很有效:
    <android.support.v7.widget.AppCompatCheckBox
    android:id="@+id/checkbox"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/custom_checkbox"
    android:button="@null"/>
    

    它将调整整个复选框(包括文本)上的图标大小。 - undefined

    1

    drawable/checkbox_custome.xml

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:state_checked="false"
            android:drawable="@drawable/checkbox_empty" />
        <item android:state_checked="true"
            android:drawable="@drawable/checkbox_check" />
    </selector>
    

    0
    看了一下https://m2.material.io/components/checkboxes/android#key-properties,我发现我们应该使用buttonuseMaterialThemeColors(也许还有app:buttonTint="@null"):
    <CheckBox
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:button="@drawable/selector_checkbox"
        android:checked="true"
        android:text="text"
        app:useMaterialThemeColors="false" />
    

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:drawable="@drawable/ic_checkbox_on" android:state_checked="true" />
        <item android:drawable="@drawable/ic_checkbox_off" android:state_checked="false" />
    </selector>
    

    enter image description here

    如果你想调整样式,请按照以下步骤操作:
    <style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
        <item name="checkboxStyle">@style/Widget.CheckBox</item>
    
    <style name="Widget.CheckBox" parent="Widget.MaterialComponents.CompoundButton.CheckBox">
        <item name="android:button">@drawable/selector_checkbox</item>
        <item name="android:textAppearance">your text appearance</item>
        <item name="useMaterialThemeColors">false</item>
    </style>
    

    网页内容由stack overflow 提供, 点击上面的
    可以查看英文原文,
    原文链接