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


当前回答

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

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

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

其他回答

我的目标是使用自定义复选框按钮并删除强调色按钮色调。我尝试了公认的答案,但现在却起作用了,这对我来说很管用:

在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:buttonTint和android:buttonTintMode

下面是一些额外的内容:

我需要使用一个自定义drawable,因此android:button设置了一个选择器drawable 我需要一些空格之间的盒子和文本,因此我遵循这个SO答案,并设置android: drawablleft和android:drawablePadding

100%鲁棒方法。

在我的情况下,我没有访问XML布局源文件,因为我从第三方MaterialDialog库获得复选框。 所以我必须通过编程来解决这个问题。

在xml中创建ColorStateList:

res /颜色/ checkbox_tinit_dark_theme.xml:

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

然后应用到复选框: ColorStateList darkStateList = ContextCompat.getColorStateList(getContext(), R.color.checkbox_tint_dark_theme); CompoundButtonCompat。darkStateList setButtonTintList(复选框);

另外,如果有人感兴趣,下面是如何从MaterialDialog对话框中获得复选框(如果你设置了.checkBoxPromptRes(…)):

CheckBox checkbox = (CheckBox) dialog.getView().findViewById(R.id.md_promptCheckbox);

希望这能有所帮助。

您可以在“colors.xml”中使用以下两个属性

<color name="colorControlNormal">#eeeeee</color>
<color name="colorControlActivated">#eeeeee</color>

colorControlNormal用于复选框的正常视图,colorControlActivated用于复选框被选中时的视图。

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

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

你可以设置复选框的android主题来获得你想要的样式的颜色。

<style name="checkBoxStyle" parent="Base.Theme.AppCompat">
    <item name="colorAccent">CHECKEDHIGHLIGHTCOLOR</item>
    <item name="android:textColorSecondary">UNCHECKEDCOLOR</item>
</style>

然后在布局文件中:

<CheckBox
     android:theme="@style/checkBoxStyle"
     android:id="@+id/chooseItemCheckBox"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"/>

不像使用android:buttonTint="@color/CHECK_COLOR"这个方法在Api 23下工作