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


当前回答

您可以使用单行代码更改复选框的颜色

android:buttonTint="@color/app_color" //任意颜色

其他回答

我更喜欢我创建的这个扩展函数:

fun CheckBox.setTint(@ColorInt color: Int?) {
    if (color == null) {
        CompoundButtonCompat.setButtonTintList(this, null)
        return
    }
    CompoundButtonCompat.setButtonTintMode(this, Mode.SRC_ATOP)
    CompoundButtonCompat.setButtonTintList(this, ColorStateList.valueOf(color))
}

它非常类似于我的ImageView,以防有人也想要它:

fun ImageView.setTint(@ColorInt color: Int?) {
    if (color == null) {
        ImageViewCompat.setImageTintList(this, null)
        return
    }
    ImageViewCompat.setImageTintMode(this, Mode.SRC_ATOP)
    ImageViewCompat.setImageTintList(this, ColorStateList.valueOf(color))
}

您应该尝试下面的代码。这对我很有用。

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/checked" 
          android:state_checked="true">
        <color android:color="@color/yello" />
    </item>
    <!-- checked -->
    <item android:drawable="@drawable/unchecked" 
          android:state_checked="false">
        <color android:color="@color/black"></color>
    </item>
    <!-- unchecked -->
    <item android:drawable="@drawable/checked" 
          android:state_focused="true">
        <color android:color="@color/yello"></color>
    </item>
    <!-- on focus -->
    <item android:drawable="@drawable/unchecked">
        <color android:color="@color/black"></color>
    </item>
    <!-- default -->
</selector>

和复选框

<CheckBox
    Button="@style/currentcy_check_box_style"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:paddingLeft="20dp"
    android:text="@string/step_one_currency_aud" />

如果你的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的可绘制性。

使用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>

您可以使用单行代码更改复选框的颜色

android:buttonTint="@color/app_color" //任意颜色