Android:设置CheckBox的颜色

Kir*_*irk 42 android android-layout

我搜索了很多地方,似乎无法找出CheckBox复选框边框的drawable.谁能指出我正确的方向?

这是未经检查的样子(几乎看不到盒子)

在此输入图像描述

这是检查状态

在此输入图像描述

这就是我想让它看起来像.

在此输入图像描述

Ram*_*ran 55

您可以使用自定义复选框xml文件.将以下xml代码保存在drawables文件夹中,将其命名为custom_checkbox.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/cbchk_blue"
        android:state_focused="false">
    </item>
    <item android:state_checked="true" 
        android:drawable="@drawable/cbchk_blue"
        android:state_focused="true">
    </item>
    <item android:state_checked="false" 
        android:drawable="@drawable/cbunchk_blue"
        android:state_focused="false">
    </item>
    <item android:state_checked="false" 
        android:drawable="@drawable/cbunchk_blue"
        android:state_focused="true">
    </item>
</selector>
Run Code Online (Sandbox Code Playgroud)

然后使用此文件作为您的复选框的背景,如下所示:

       <CheckBox
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:button="@drawable/custom_checkbox"
        android:id="@+id/checkBox" />
Run Code Online (Sandbox Code Playgroud)

在这里,我上传我自己的图像,我用它代替cbchk_bluecbunchk_blue

未经检查的CheckBox 检查CheckBox

  • 如果像我这样的一些菜鸟找到了这个答案而且现在不知道如何使用它:在drawable目录中创建一个xml文件,复制粘贴上面的编码,并将创建的样式设置为button-property(xml的文件名) = style-name)为您的复选框.有关详细信息,请参阅http://developer.android.com/guide/topics/resources/drawable-resource.html#StateList (5认同)

Yur*_*aev 34

当您使用主题Holo Dark for Activity和白色背景时,也会发生同样的问题.所以复选框有黑暗风格.简单的解决方法是从Android的Holo Light直接设置背景:

int id = Resources.getSystem().getIdentifier("btn_check_holo_light", "drawable", "android");
checkBox.setButtonDrawable(id);
Run Code Online (Sandbox Code Playgroud)

您可以在以下答案中找到所有这些内容的完整概述:https: //stackoverflow.com/a/10139809/1170154

  • 这是我正在寻找的机器人.谢谢!! (2认同)

Pho*_*oca 27

从Android 5和API 21级开始,可以自由选择复选框(以及许多其他小部件)的颜色.将以下内容添加到您的values-v21/styles.xml(同时确保您在以下API中有后备values/styles.xml:

<style name="CustomCheckBox">
    <item name="android:theme">@style/CheckBoxAppTheme</item>
</style>

<style name="CheckBoxAppTheme">
    <item name="android:colorAccent">
        @color/theFillColorInCheckedState
    </item>
    <item name="android:colorControlNormal">
        @color/theBorderColorInUncheckedState
    </item>
    <item name="android:colorControlHighlight">
        @color/theBackgroundColorWhenFocusingTheCheckBox
    </item>
</style>
Run Code Online (Sandbox Code Playgroud)

然后,您只需将样式应用于布局中的复选框:

<CheckBox
    style="@style/CustomCheckBox" />
Run Code Online (Sandbox Code Playgroud)

就是这样,复选框以您喜欢的颜色显示!

  • 我想你缺少`android:colorControlActivated`. (4认同)

Aar*_*ley 15

好的,所以我很抱歉,但大部分答案都不完整或者有一些小错误.跨越不同版本的Android的"样式"控件是一个史诗般的痛苦.在一个项目设计受到严格限制的情况下拔掉头发几天之后,我终于崩溃并编写了一个测试应用程序然后真正挖掘并测试了各种解决方案,用于样式开关和复选框,因为当一个设计有一个它经常有另一个.这是我发现的......

第一:你实际上不能设置其中任何一个,但你可以将一个主题应用于所有这些,或者只是其中一个.

第二:您可以从XML完成所有操作,而不需要第二个值-v21/styles.xml.

第三:当涉及到交换机时,如果你想支持旧版本的Android(比如我确定你这样做),你有两个基本的选择......

  1. 您可以使用a,SwitchCompat并且您可以使它在不同平台上看起来相同.
  2. 你可以使用a Switch,你可以用你的主题的其余部分来主题,或者只是那个特定的开关,在旧版本的Android上,你只会看到一个没有样式的旧方形开关.

现在好了,简单的参考代码.再次,如果您创建一个简单的Hello World!并删除此代码,你可以发挥你的心灵内容.所有这些都是锅炉板,所以我只是要包含活动和风格的XML ......

activity_main.xml中...

<?xml version="1.0" encoding="utf-8"?>
Run Code Online (Sandbox Code Playgroud)

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.kunai.switchtest.MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="'Styled' SwitchCompat" />

    <android.support.v7.widget.SwitchCompat
        android:id="@+id/switch_item"
        android:layout_width="wrap_content"
        android:layout_height="46dp"
        android:layout_alignParentEnd="true"
        android:layout_marginEnd="16dp"
        android:checked="true"
        android:longClickable="false"
        android:textOff="OFF"
        android:textOn="ON"
        app:switchTextAppearance="@style/BrandedSwitch.text"
        app:theme="@style/BrandedSwitch.control"
        app:showText="true" />

</RelativeLayout>

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.kunai.switchtest.MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Themed SwitchCompat" />

    <android.support.v7.widget.SwitchCompat
        android:id="@+id/switch_item2"
        android:layout_width="wrap_content"
        android:layout_height="46dp"
        android:layout_alignParentEnd="true"
        android:layout_marginEnd="16dp"
        android:checked="true"
        android:longClickable="false" />

</RelativeLayout>

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.kunai.switchtest.MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Themed Switch" />

    <Switch
        android:id="@+id/switch_item3"
        android:layout_width="wrap_content"
        android:layout_height="46dp"
        android:layout_alignParentEnd="true"
        android:layout_marginEnd="16dp"
        android:checked="true"
        android:longClickable="false"
        android:textOff="OFF"
        android:textOn="ON"/>

</RelativeLayout>

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.kunai.switchtest.MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="'Styled' Switch" />

    <Switch
        android:id="@+id/switch_item4"
        android:layout_width="wrap_content"
        android:layout_height="46dp"
        android:layout_alignParentEnd="true"
        android:layout_marginEnd="16dp"
        android:checked="true"
        android:longClickable="false"
        android:textOff="OFF"
        android:textOn="ON"
        android:theme="@style/BrandedSwitch"/>

</RelativeLayout>

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.kunai.switchtest.MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="'Styled' CheckBox" />

    <CheckBox
        android:id="@+id/checkbox"
        android:layout_width="wrap_content"
        android:layout_height="46dp"
        android:layout_alignParentEnd="true"
        android:layout_marginEnd="16dp"
        android:checked="true"
        android:longClickable="false"
        android:theme="@style/BrandedCheckBox"/>

</RelativeLayout>

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.kunai.switchtest.MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Themed CheckBox" />

    <CheckBox
        android:id="@+id/checkbox2"
        android:layout_width="wrap_content"
        android:layout_height="46dp"
        android:layout_alignParentEnd="true"
        android:layout_marginEnd="16dp"
        android:checked="true"
        android:longClickable="false"/>

</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

styles.xml ...

<resources>

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">#3F51B5</item>
    <item name="colorPrimaryDark">#303F9F</item>
    <item name="colorAccent">#FF4081</item>
</style>

<style name="BrandedSwitch.control" parent="Theme.AppCompat.Light">
    <!-- active thumb & track color (30% transparency) -->
    <item name="colorControlActivated">#e6e600</item>
    <item name="colorSwitchThumbNormal">#cc0000</item>
</style>

<style name="BrandedSwitch.text" parent="Theme.AppCompat.Light">
    <item name="android:textColor">#ffa000</item>
    <item name="android:textSize">9dp</item>
</style>

<style name="BrandedCheckBox" parent="AppTheme">
    <item name="colorAccent">#aaf000</item>
    <item name="colorControlNormal">#ff0000</item>
</style>

<style name="BrandedSwitch" parent="AppTheme">
    <item name="colorAccent">#39ac39</item>
</style>
Run Code Online (Sandbox Code Playgroud)

我知道,我知道,你太懒于构建它,你只想写你的代码.我知道了.这是你运行时的样子......

API_21:

API 21

API_18:

API18


Key*_*ani 7

您可以像这个API21及以上一样设置CHECKBOX颜色

机器人:buttonTint = "@颜色/ YOUR_COLOR"

<CheckBox
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:buttonTint="@color/YOUR_COLOR" />
Run Code Online (Sandbox Code Playgroud)

对于旧版本支持,使用V7库的AppCompatCheckBox

应用程式:buttonTint = "@颜色/ YOUR_COLOR"

<android.support.v7.widget.AppCompatCheckBox 
    android:layout_height="wrap_content"
    android:layout_width="match_parent"  
    app:buttonTint="@color/YOUR_COLOR" /> 
Run Code Online (Sandbox Code Playgroud)


Dhe*_*.S. 6

它由drawables指定:android.R.drawable.checkbox_off_backgroundandroid.R.drawable.checkbox_on_background

  • 那么如何改变那种颜色呢? (15认同)