我如何改变默认的复选框的颜色在Android? 默认情况下,复选框的颜色是绿色,我想改变这个颜色。 如果不可能,请告诉我如何做一个自定义复选框?


当前回答

我遇到了同样的问题,我用下面的技术得到了一个解决方案。将btn_check.xml从android-sdk/platforms/android-#(version)/data/res/drawable复制到项目的drawable文件夹,并将“on”和“off”图像状态更改为自定义图像。 然后你的xml将只需要android:button="@drawable/btn_check"

<CheckBox
    android:button="@drawable/btn_check"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:checked="true" />

如果你想使用不同的默认Android图标,你可以使用:

android:button="@android:drawable/..."

其他回答

我建议在android中使用风格方法来配置内置的android视图,在你的项目中添加新的风格:

<style name="yourStyle" parent="Base.Theme.AppCompat">
    <item name="colorAccent">your_color</item> <!-- for uncheck state -->
    <item name="android:textColorSecondary">your color</item> <!-- for check state -->
</style>

并添加assign this style到复选框的主题: android:主题= " @style / youStyle”

希望这能有所帮助。

使用buttonTint更改按钮和颜色选择器的颜色以上21+ api版本。

<android.support.v7.widget.AppCompatCheckBox
                android:id="@+id/check"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                app:buttonTint="@color/checkbox_filter_tint"
                tools:targetApi="21"/>

res /颜色/ checkbox_filter_tint。xml

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

在res-> Drawable下创建一个xml Drawable资源文件,并命名为checkbox_custom_01.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_custom_01_checked_white_green_32" />
  <item 
   android:state_checked="false" 
   android:drawable="@drawable/checkbox_custom_01_unchecked_gray_32" />
</selector>

上传您的自定义复选框图像文件(我建议png)到您的res->可绘制文件夹。

然后进入布局文件并将复选框更改为

<CheckBox
    android:id="@+id/checkBox1"
    android:button="@drawable/checkbox_custom_01"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:focusable="false"
    android:focusableInTouchMode="false"
    android:text="CheckBox"
    android:textSize="32dip"/>

你可以自定义任何东西,只要android:button指向你之前创建的正确的XML文件。

新手注意:虽然这不是强制性的,但是在你的整个布局树中用唯一的id来命名你的复选框是一个很好的做法。

如果你的minSdkVersion是21+使用android:buttonTint属性来更新复选框的颜色:

<CheckBox
  ...
  android:buttonTint="@color/tint_color" />

在使用AppCompat库和支持低于21的Android版本的项目中,你可以使用buttonTint属性的compat版本:

<CheckBox
  ...
  app:buttonTint="@color/tint_color" />

在这种情况下,如果你想子类化一个CheckBox,不要忘记使用AppCompatCheckBox代替。

之前的回答:

你可以使用android:button="@drawable/your_check_drawable"属性来改变checkboxes的可绘制性。

如果你打算使用android图标,如上所述..

android:button="@android:drawable/..."

. .这是一个不错的选择,但为了让它工作-我发现你需要添加切换逻辑来显示/隐藏复选标记,就像这样:

    checkBoxShowPwd.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

        // checkbox status is changed from uncheck to checked.
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

            int btnDrawable = android.R.drawable.checkbox_off_background;

            if (isChecked)
            {
                btnDrawable = android.R.drawable.checkbox_on_background;
            }

            checkBoxShowPwd.setButtonDrawable(btnDrawable);

        }
    });