如何在Android中更改CheckBox的颜色

316
如何在Android中更改默认的CheckBox颜色? 默认情况下,CheckBox的颜色是绿色的,我想要改变这个颜色。 如果不可能的话,请告诉我如何制作自定义的CheckBox?

1
你是想改变字体的颜色吗?还是实际框的颜色? - user725913
1
实际的盒子颜色我改变了。 - Piyush
90
android:buttonTint="@color/mybrown" 进行设置是改变方框颜色的简单方法。 - shauvik
6
@Shauvik,它只是在使用物料设计工作 :) - Mucahit
12
最好使用app:buttonTint="@color/mybrown"来代替,这样可以在API < 21上工作。 - Nikaoto
显示剩余2条评论
27个回答

2

我的目标是使用自定义复选框按钮并 去掉强调颜色按钮的色调。我尝试了被接受的答案,但没有奏效,相反,这个对我有用:

styles.xml 文件中

<style name="Theme.Checkbox" parent="Widget.AppCompat.CompoundButton.CheckBox">
    <item name="android:button">@drawable/theme_checkbox_button</item>
    <item name="android:drawableLeft">@android:color/transparent</item>
    <item name="android:drawablePadding">10dp</item>
    <item name="android:buttonTint">@android:color/transparent</item>
    <item name="android:buttonTintMode">screen</item>
</style>

现在对于这个问题,只需要使用android:buttonTintandroid:buttonTintMode两个必要属性。

现在是一些额外的内容:

  • 我需要使用自定义drawable,因此android:button设置为一个选择器drawable。
  • 我需要在框和文本之间留出一些间距,因此我遵循了这个SO答案并设置了android:drawableLeftandroid:drawablePadding

2

你可以尝试下面的代码,它对我有效。

<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" />

1

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

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))
}

1
如果您要使用如上所述的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);

        }
    });

这是一项不必要的额外工作。在可绘制的 XML 文件中,您可以为选择器提供对开启或关闭可绘制资源的引用。这将根据复选框的状态自动放置正确的可绘制内容。 - jkincali

1

您可以通过将<CheckBox>嵌入到<LinearLayout>中来更改其背景颜色。然后将<LinearLayout>的背景颜色更改为所需的颜色。


问题是关于复选框的颜色,而不是背景。 - Zvi

0
使用ColorStateList来更改色调颜色。
fun CheckBox.tint(colors: ColorStateList) {
    buttonTintList = colors
}

fun CheckBox.tint(@ColorInt color: Int) = tint(context.colorStateList(color))

其他一般材料视图。
fun SeekBar.tint(@ColorInt color: Int) {
    val s1 = ColorStateList.valueOf(color)
    thumbTintList = s1
    progressTintList = s1
}

fun ProgressBar.tint(@ColorInt color: Int, skipIndeterminate: Boolean = false) {
    val sl = ColorStateList.valueOf(color)
    progressTintList = sl
    secondaryProgressTintList = sl
    if (!skipIndeterminate) indeterminateTintList = sl
}

-3

您可以使用以下代码行在Java代码中动态更改复选框的背景。

//Setting up background color on checkbox.
 checkbox.setBackgroundColor(Color.parseColor("#00e2a5"));

这个答案来自于网站。你也可以使用网站将你的RGB颜色转换为十六进制值,你需要提供parseColor。


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