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


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

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

我遇到了同样的问题,我用下面的技术得到了一个解决方案。将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/..."

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

<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图标,如上所述..

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

        }
    });

在你的styles.xml文件中添加这一行:

<style>
    <item name="android:colorAccent">@android:color/holo_green_dark</item>
</style>

您可以直接在XML中更改颜色。使用buttonTint的盒子:(API级别23)

<CheckBox
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:buttonTint="@color/CHECK_COLOR" />

你也可以使用旧API级别的appCompatCheckbox v7来做到这一点:

<android.support.v7.widget.AppCompatCheckBox 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    app:buttonTint="@color/COLOR_HERE" /> 

你可以设置复选框的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下工作

在xml中添加buttonTint

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

程序版本:

int [][] states = {{android.R.attr.state_checked}, {}};
int [] colors = {color_for_state_checked, color_for_state_normal}
CompoundButtonCompat.setButtonTintList(checkbox, new ColorStateList(states, colors));

你可以创建自己的XML在drawable和使用这作为android:background="@drawable/your_xml"

这样你就可以给边角任何东西

<item>
    <shape>
        <gradient

            android:endColor="#fff"
            android:startColor="#fff"/>
        <corners
            android:radius="2dp"/>
        <stroke
            android:width="15dp"
            android:color="#0013669e"/>

    </shape>
</item>

在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来命名你的复选框是一个很好的做法。

我建议在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”

希望这能有所帮助。

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

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

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

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

嗨,这是黑暗主题和光明主题的主题代码。

<attr name="buttonsearch_picture" format="reference"/>
<attr name="buttonrefresh_picture" format="reference"/>

<style name="Theme.Light" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowBackground">@color/white</item>
    <item name="android:windowActionBar">false</item>
    <item name="android:textColorPrimary">@color/black</item>
    <item name="android:textColorSecondary">@color/black</item>
    <item name="android:textColor">@color/material_gray_800</item>
    <item name="actionOverflowButtonStyle">@style/LightOverflowButtonStyle</item>
    <item name="buttonsearch_picture">@drawable/ic_search_black</item>
    <item name="buttonrefresh_picture">@drawable/ic_refresh_black</item>
</style>

<style name="Theme.Dark" parent="Theme.AppCompat.NoActionBar">
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowBackground">@color/white</item>
    <item name="android:windowActionBar">false</item>
    <item name="android:textColorPrimary">@color/white</item>
    <item name="android:textColorSecondary">@color/material_gray_500</item>
    <item name="android:textColor">@color/material_gray_800</item>
    <item name="actionOverflowButtonStyle">@style/DarkOverflowButtonStyle</item>
    <item name="buttonsearch_picture">@drawable/ic_search_white</item>
    <item name="buttonrefresh_picture">@drawable/ic_refresh_white</item>
    <item name="android:colorBackground">#ffffff</item>
    <item name="android:alertDialogTheme">@style/LightDialogTheme</item>
    <item name="android:alertDialogStyle">@style/LightDialogTheme</item>
  <!-- <item name="android:textViewStyle">@style/AppTheme.Widget.TextView</item>-->
    <item name="android:popupMenuStyle">@style/PopupMenu</item>
</style>

如果你想改变复选框的颜色,那么“colorAccent”属性将用于选中状态,“android:textColorSecondary”将用于取消选中状态。

"actionOverflowButtonStyle"将用于改变动作栏中溢出图标的颜色。

"buttonsearch_picture"属性将用于改变操作栏中操作按钮的颜色。这是style.xml中的自定义属性

<attr name="buttonsearch_picture" format="reference"/>

同样是刷新按钮,我在我的应用程序中使用。

"android:popupMenuStyle"属性用于在Dark主题中获得Light主题弹出式菜单样式。

<style name="PopupMenu" parent="Theme.AppCompat.Light.NoActionBar">
</style>

这是工具栏代码,我正在使用在我的岩石播放器应用程序。

 <android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    app:contentInsetStart="0dp"
    android:title="Rocks Player"
    android:layout_width="match_parent"
    android:elevation="4dp"
    android:layout_height="48dp"
    app:layout_scrollFlags="scroll|enterAlways"
    android:minHeight="48dp"
    app:titleTextAppearance="@style/Toolbar.TitleText"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
    android:background="?attr/colorPrimary"
    >
</android.support.v7.widget.Toolbar>

主题:-

 <style name="AppTheme0" parent="Theme.Light">
    <item name="colorPrimary">#ffffff</item>
    <item name="colorPrimaryDark">#cccccc</item>
    <item name="colorAccent">#0294ff</item>
</style>

<style name="AppTheme1" parent="Theme.Dark">
    <item name="colorPrimary">#4161b2</item>
    <item name="colorPrimaryDark">#4161b2</item>
    <item name="colorAccent">#4161b2</item>
</style>

buttonTint工作为我尝试

android:buttonTint=“@color/white”

<CheckBox
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:id="@+id/agreeCheckBox"
    android:text="@string/i_agree_to_terms_s"
    android:buttonTint="@color/white"
    android:layout_below="@+id/avoid_spam_text"/>

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

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

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

希望这能有所帮助。

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

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

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

通过设置ColorStateList,有一种更简单的方法以编程方式设置颜色。

 Checkbox.setButtonTintList(ColorStateList.valueOf(getContext().getColor(R.color.yourcolor)))

大多数答案都通过xml文件。如果你发现大多数Android版本都有一个活跃的答案,并且两种状态都只有一种颜色,检查和取消检查:下面是我的解决方案:

科特林:

val colorFilter = PorterDuffColorFilter(Color.CYAN, PorterDuff.Mode.SRC_ATOP)
CompoundButtonCompat.getButtonDrawable(checkBox)?.colorFilter = colorFilter

Java:

ColorFilter colorFilter = new PorterDuffColorFilter(Color.CYAN, PorterDuff.Mode.SRC_ATOP);
Drawable drawable = CompoundButtonCompat.getButtonDrawable(checkBox);
if (drawable != null) {
    drawable.setColorFilter(colorFilter);
}

我的minSdkVersion是15,我的BaseAppTheme父是Theme.AppCompat.Light.NoActionBar,我正在以编程方式创建我的复选框。下面的步骤对我很有效。

1. 在Java代码中进行更改

CheckBox checkBox = new CheckBox(context);

to

AppCompatCheckBox checkBox = new AppCompatCheckBox(context);

2. 在你的styles.xml中,添加:

<style name="MyCheckboxStyle" parent="Widget.AppCompat.CompoundButton.CheckBox">
    <item name="buttonTint">@color/primary_dark</item>
</style>

3.最后,在你的BaseAppTheme(或AppTheme)样式中,添加:

<item name="checkboxStyle">@style/MyCheckboxStyle</item>
<item name="android:checkboxStyle">@style/MyCheckboxStyle</item>

如果textColorSecondary不适合你,你可能已经在你的主题中定义了colorControlNormal为不同的颜色。如果有,就用吧

<style name="yourStyle" parent="Base.Theme.AppCompat">
    <item name="colorAccent">your_checked_color</item> <!-- for checked state -->
    <item name="colorControlNormal">your_unchecked_color</item> <!-- for unchecked state -->
</style>

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

在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

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

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