Changing Default Color of Android CheckBox Check Mark

前端 未结 2 1402
Happy的楠姐
Happy的楠姐 2021-01-11 11:35

How do I change the default color of the Android checkbox from green checkmarks to blue for a particular CheckBox?

相关标签:
2条回答
  • 2021-01-11 12:21

    Unfortunately, changing the colour isn't a simple attribute. The checkmark is an image, so you have to create a custom image. Take a look at this example

    Create a selector xml file such as this:

    <?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/star_down" />
        <item android:state_checked="false" android:drawable="@drawable/star" />
    </selector>
    

    save this xml file in your res\drawables\ folder. Then inside your layout file apply it to your checkBox like this:

    <CheckBox
        android:text="Custom CheckBox"
        android:button="@drawable/checkbox_selector"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
    

    In this example you'd name your selector xml file "checkbox_selector.xml" and you'd need a star_down.png, and star.png in your drawables folder as well. You can use this technique to create different colored checkboxes by altering the system checkbox images to whatever color you want and referencing the altered png files in a selector.

    0 讨论(0)
  • 2021-01-11 12:33

    This is easy to do in xml using buttonTint (as of API level 23):

    <CheckBox
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:buttonTint="@color/COLOR_HERE" />
    

    and as Nicolás pointed out, you can do this using appCompatCheckbox v7 for older APIs:

    <android.support.v7.widget.AppCompatCheckBox 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        app:buttonTint="@color/COLOR_HERE" /> 
    
    0 讨论(0)
提交回复
热议问题