复选框颜色变化 Android

3
我有一个复选框,想要更改它的颜色。我附上了一张图片以便更好地理解。enter image description here 我想把黑色正方形的颜色改成白色,就像文本“按下跳过”。我的xml代码如下:
<CheckBox
 android:id="@+id/checkBox1"
 android:background="@drawable/tutorial_screen_edit_text_style"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:onClick="onCheckboxClicked"
 android:text="Press to Skip"
 android:textColor="@color/white"

 android:layout_alignParentTop="true"
 android:layout_alignParentRight="true"
 android:layout_alignParentEnd="true" />
4个回答

4
答案非常简单,查看以下代码片段:
```html

答案非常简单,查看以下代码片段:

```
<CheckBox
android:id="@+id/checkBox1"
android:background="@drawable/tutorial_screen_edit_text_style"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onCheckboxClicked"
android:text="Press to Skip"
android:buttonTint="@color/white"    <!--just add this line-->
android:textColor="@color/white"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />

1
非常感谢你,兄弟!你是最棒的,帮我省了很多时间 :) - Ali Nasir
总是乐意帮助兄弟 :) - Saim Mahmood

3

尝试使用ColorStateList。

    ColorStateList  colorStateList = new ColorStateList(
        new int[][]{
                new int[]{android.R.attr.state_checked}, // unchecked
                new int[]{android.R.attr.state_checked} , // checked
        },
        new int[]{
                Color.parseColor("#000000"),  //unchecked color
                Color.parseColor("#FFFFFF"),  //checked color
        }
);

使用setButtonTintList()设置颜色。

    CheckBox cb = (CheckBox) findViewById(R.id.checkBox1);
 CompoundButtonCompat.setButtonTintList(cb,colorStateList);

这也适用于minSdk 21到30吗? - mochadwi
@mochadwi 是的,setButtonTIntList() 的最低 SDK 版本是 21。 https://developer.android.com/reference/android/widget/CompoundButton#setButtonTintList(android.content.res.ColorStateList) - rafsanahmad007
太棒了,谢谢!我这边不再需要使用 compoundDrawableTintList 了。之前还在使用 compoundDrawableTintList,但在运行时修改时它似乎不起作用。 - mochadwi
另一方面,setButtonTintList 在运行时可以无缝工作,例如:button.setOnClickListener { checkbox.setButtonTintList(..., ...) } - mochadwi

0

在drawable文件夹中定义自己的可绘制对象

checkbox_selector.xml

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

your_layout.xml

<CheckBox
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:button="@drawable/checkbox_selector"
    android:text="CheckBox"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:textColor="@color/Black" />

我不理解drawable部分,它们是用来做什么的? - Ali Nasir
你需要为每个生成的 checkBox 找到白色正方形图像,你可以使用此链接生成它们:https://android-material-icon-generator.bitdroid.de/#section-material-icons - Farid
您可以使用以下链接获取复选框和已选复选框的绘图:http://www.clker.com/cliparts/B/2/v/i/n/T/tick-check-box-md.png 和 http://www.clker.com/cliparts/e/q/p/N/s/G/checkbox-unchecked-md.png。 - sadat

0

试试这个。

     <CheckBox
                        android:id="@+id/chk_remember_signup"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:buttonTint="@android:color/white"
                        android:text="hello" />

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