Android appcompat-v7:21.0.0更改材料复选框颜色

时间:2014-11-10 11:44:50

标签: android material-design

我已经更新了我的项目以使用最新的appcompat支持库,新版本使用了材料设计复选框和单选按钮。我的应用程序是黑暗的主题,复选框是黑色,很难看到。我试图根据Maintaining Compatibility改变颜色,但到目前为止没有任何作用。

RES /值/ styles.xml

  <style name="AppBaseTheme" parent="@style/Theme.AppCompat.Light">

    <!-- customize the color palette -->
    <item name="colorAccent">@color/silver</item>
  </style>
build.gradle中的

    android {
      compileSdkVersion 21
      buildToolsVersion '21.1.1'

      defaultConfig {
        minSdkVersion 9
        targetSdkVersion 19
      }
    }

.....
.....    

    compile 'com.android.support:appcompat-v7:21.0.0'

的AndroidManifest.xml:

  <application
      android:name="ee.mtakso.App"
      android:allowBackup="true"
      android:icon="@drawable/ic_launcher"
      android:label="@string/app_name"
      android:theme="@style/AppBaseTheme">

复选框,editTexts,radiobuttons等仍为黑色。

修改

我不知道这是否会产生太大影响,但我使用的单选按钮和复选框是CheckedTextView,如下所示:

单一(单选按钮): android:checkMark="?android:attr/listChoiceIndicatorSingle"

多重(复选框): android:checkMark="?android:attr/listChoiceIndicatorMultiple"

由于这些黑色材料可以绘制,我不认为问题来自于它们。

18 个答案:

答案 0 :(得分:123)

我遇到了与未经检查的CheckBoxes和RadioButtons 类似的问题。 当我发现控件从

中取出“关闭”颜色时,我找到了解决方案

<item name="android:textColorSecondary">@color/secondary_text</item>


修改

指定,如果您的应用或活动的主题继承了L's AppCompat(Dark / Light / Light.DarkActionBar)之一,您可以设置:

<style name="SampleTheme" parent="Theme.AppCompat">
    <item name="colorAccent">@color/green</item>
    <item name="android:textColorSecondary">@color/red</item>
</style>

这就是结果:

enter image description here

注意:当您获得不同的效果时,您可能会使用“错误的”主题 - 请确保正确设置。

答案 1 :(得分:35)

我认为这是AppCompat主题中的错误。我的解决方法是在xml布局文件中为每个CheckBox添加两行代码。

android:button="@drawable/abc_btn_check_material"
android:buttonTint="@color/colorAccent"

你真的不想直接引用abc_ drawables,但在这种情况下我找不到其他解决方案。

  

这也适用于RadioButton小部件!你只需要使用    abc_btn_radio_material 而不是 abc_btn_check_material

答案 2 :(得分:17)

我这样做至少改变了一个复选框的边框:

<style name="checkBoxComponent" parent="AppTheme">
    //Checked color
    <item name="colorAccent">@color/blueBackground</item> 
    //Checkbox border color
    <item name="android:textColorSecondary">@color/grayBorder</item> 
</style>

在我的布局中

<android.support.v7.widget.AppCompatCheckBox
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:theme="@style/checkBoxComponent"
                        android:text="Yay" />

仍在弄清楚如何获得复选框的背景。 希望它有所帮助。

答案 3 :(得分:14)

我和你有同样的问题。 我再次看了AppCompat v21 — Material Design for Pre-Lollipop Devices!

我发现这个“你的所有活动必须从 ActionBarActivity 延伸,它从v4支持库的FragmentActivity扩展,所以你可以继续使用片段。”

所以我将我的活动改为ActionBarActivity,它解决了我的问题。我希望它也会解决你的问题。

答案 4 :(得分:8)

我一直在寻找解决方案而且我找到了它。

第1步
扩展ActionBarActivity

public static class MyActivity extends ActionBarActivity {
        //...
}

第2步
在您的样式文件中写入两个值

<style name="MyTheme" parent="Theme.AppCompat.NoActionBar">
        <item name="colorAccent">#009688</item>
        <item name="android:textColorSecondary">#757575</item>
</style>

colorAccent - 选中颜色
android:textColorSecondary - 未选中的颜色

第3步
在AndroidManifest.xml中设置主题

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="com.test.radiobutton"
          android:versionCode="1"
          android:versionName="1.0">
    <application android:label="@string/app_name" android:icon="@drawable/ic_launcher">

                    ....

        <activity android:name=".activity.MyActivity "
                  android:theme="@style/MyTheme"/>

                  .....

    </application>
</manifest>



RESULT
Android 5
android_5
Android 4.2.2
android_4.2.2

答案 5 :(得分:7)

<强> 1。在styles.xml文件中声明自定义样式。

<style name="CustomStyledRadioButton" parent="Theme.AppCompat.Light">
    <item name="colorControlNormal">@color/red</item>
    <item name="colorControlActivated">@color/red_pressed</item>
</style>

<强> 2。通过android:theme属性将此样式应用于RadioButton。

  <RadioButton  android:id="@+id/rb_option1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:checked="true"
    android:gravity="left"
    android:lineSpacingExtra="10dp"
    android:padding="10dp"
    android:text="Option1"
    android:textColor="@color/black"
    android:theme="@style/CustomStyledRadioButton"/>

答案 6 :(得分:5)

100%正常工作

只需为您的RadioButton创建一个样式并更改colorAccent,如下所示:

<style name="RadioButtonTeal" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="colorAccent">@color/md_teal_600</item>
</style>

然后只需将此样式添加到AppCompatRadioButton:

<android.support.v7.widget.AppCompatRadioButton
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:theme="@style/RadioButtonTeal" />

答案 7 :(得分:4)

要指定颜色,请覆盖:

用于检查颜色:

<item name="android:colorControlActivated">@color/your_color</item> 

表示颜色不清晰:

<item name="android:colorControlNormal">@color/your_color</item>

答案 8 :(得分:3)

兼容性库会出现很多问题,包括复选框和单选按钮。

1)对于任何Android 4.x设备,它们仅为黑色。它们在Android 5.x和2.x中都可以使用(不要问我为什么它在2.x上工作,没有线索)。

2)他们没有处于禁用状态(如果启用了所有复选框,则无关紧要,否则您会因为非常糟糕的意外而感到高兴。)

请注意,默认的黑暗主题背景是灰色的,而不是黑色,所以如果你保持默认,那么&#34; ok&#34;。

为了解决这个问题,我创建了以下drawables的白色和禁用版本,全部添加到我的核心项目中,但不是在compat项目中(为了明显的维护目的):

abc_btn_check_to_on_mtrl_000
abc_btn_check_to_on_mtrl_015
abc_btn_radio_to_on_mtrl_000
abc_btn_radio_to_on_mtrl_015

然后创建了一个drawable来管理所有状态(以覆盖compat原始状态):

例如,黑暗主题将使用此:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_enabled="false" android:state_checked="true" android:drawable="@drawable/abc_btn_check_to_on_mtrl_015_disabled" />
    <item android:state_enabled="true" android:state_checked="true" android:drawable="@drawable/abc_btn_check_to_on_mtrl_015" />
    <item android:state_enabled="true" android:drawable="@drawable/abc_btn_check_to_on_mtrl_000" />
    <item android:drawable="@drawable/abc_btn_check_to_on_mtrl_000_disabled" />
</selector>

灯光主题将使用此功能(用户可以在我的应用中切换主题):

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_enabled="false" android:state_checked="true" android:drawable="@drawable/abc_btn_check_to_on_mtrl_015_disabled" />
    <item android:state_enabled="true" android:state_checked="true" android:drawable="@drawable/abc_btn_check_to_on_mtrl_015_light" />
    <item android:state_enabled="true" android:drawable="@drawable/abc_btn_check_to_on_mtrl_000_light" />
    <item android:drawable="@drawable/abc_btn_check_to_on_mtrl_000_disabled" />
</selector>

然后我所要做的就是覆盖默认的compat主题(活动和对话,对于明/暗主题),添加如下内容:

    <item name="android:listChoiceIndicatorSingle">@drawable/abc_btn_radio_material</item>
    <item name="android:listChoiceIndicatorMultiple">@drawable/abc_btn_check_material</item>

这适用于轻型主题:

    <item name="android:listChoiceIndicatorSingle">@drawable/abc_btn_radio_material_light</item>
    <item name="android:listChoiceIndicatorMultiple">@drawable/abc_btn_check_material_light</item>

现在,我在每个Android版本上都拥有完全可操作的复选框和无线电! IMO compat库根本没有测试黑暗主题,只使用了白色主题。禁用状态可能永远不会被compat lib的开发者使用。

答案 9 :(得分:2)

只需扩展 ActionBarActivity

Public class MainActivity extends ActionBarActivity {

//...

}

答案 10 :(得分:1)

您可以将另一个主题传递给警告对话框的构造

<style name="RadioButton" parent="Theme.AppCompat.Light.Dialog.Alert">
    <item name="colorControlNormal">@color/red</item>
    <item name="colorControlActivated">@color/green</item>
</style>

并在构造函数中使用该样式

new AlertDialog.Builder(getContext(), R.style.RadioButton);

答案 11 :(得分:1)

你所做的应该根据android博客帖子工作:

colorAccent :主要品牌颜色的明亮补充。默认情况下,这是应用于框架控件的颜色(通过colorControlActivated)。

colorControlActivated :在激活(例如已检查)状态下应用于框架控件的颜色。

也许问题来自你的@ style / Theme.AppCompat.Light作为父主题的主题,只需使用Theme.AppCompat尝试:

<style name="AppBaseTheme" parent="Theme.AppCompat.Light">
    <!-- customize the color palette -->
    <item name="colorAccent">@color/silver</item>
</style>

答案 12 :(得分:1)

如果你想更改特定的复选框背景颜色(不是整个应用程序),你可以尝试这个技巧:

在drawable文件夹中为后台创建custom_checkbox.xml文件:

 <shape xmlns:android="http://schemas.android.com/apk/res/android">
     <stroke android:width="14dp" android:color="@android:color/transparent"  />
     <solid android:color="#ffffff" /> //the intended color of the background
     <corners android:radius="2dp" />
 </shape> 

然后将其设置为复选框的背景:

<CheckBox
    android:id="@+id/rebate_tnc_checkbox"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/custom_checkbox" />

答案 13 :(得分:0)

添加

<item name="android:textColorSecondary">@color/secondary_text</item>

在style.xml和style.xml(v21)

答案 14 :(得分:0)

添加此语法以为复选框或单选按钮添加窗口小部件 机器人:buttonTint = “@颜色/银”

答案 15 :(得分:0)

如果您尝试手动创建ComboBox(新的ComboBox()...),那么无论您设置什么,它们总是会变黑。很明显,compat库已经坏了,我已经为此创建了一个错误报告:https://code.google.com/p/android/issues/detail?id=158020

答案 16 :(得分:0)

我正在使用appcompat库v21:

<style name="AppBaseTheme" parent="Theme.AppCompat.Light">
        <item name="colorPrimary">@color/custom_color</item>
        <item name="colorAccent">@color/custom_color</item>    
</style>

上面的样式使我的复选框显示为材料设计(在android 5,4.3,4.1.1上测试),但在android 2.3.3上显示旧复选框样式。

答案 17 :(得分:0)

AppTheme的父级是@style/Theme.AppCompat.Light

将其更改为@style/Theme.AppCompat

现在你的控件在黑暗的背景下会很亮。

相关问题