首页 文章

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

提问于
浏览
61

我试图根据Maintaining Compatibility改变颜色,但到目前为止没有任何作用 .

res/values/styles.xml

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

    <!-- customize the color palette -->
    <item name="colorAccent">@color/silver</item>
  </style>

in 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等保持黑色 .

编辑

我't know if this makes much difference, but the radiobuttons and checkboxes I'使用的是 CheckedTextView ,如下:

Single (radio button): android:checkMark="?android:attr/listChoiceIndicatorSingle"

Multi (check box): android:checkMark="?android:attr/listChoiceIndicatorMultiple"

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

18 回答

  • 35

    添加此语法为复选框或单选按钮android设置颜色:buttonTint =“@ color / silver”

  • 3

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

    在style.xml和style.xml中(v21)

  • 0

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

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

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

    将其更改为 @style/Theme.AppCompat .

    现在你的控件在深色背景上会很亮 .

  • 1

    我和 unchecked CheckBoxes and RadioButtons 有类似的问题 . 当我发现控件从中获取"Off"颜色时,我找到了解决方案

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


    EDIT:

    指定,如果您的应用或活动的主题继承了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

    Notice: 当您获得不同的效果时,您可能使用"wrong"主题 - 请确保正确设置 .

  • 0

    我相信这是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

  • 0

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

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

    仍在弄清楚如何获得复选框的背景 . 希望能帮助到你 .

  • 7

    我遇到了和你一样的问题 . 我又看了一眼AppCompat v21 — Material Design for Pre-Lollipop Devices!

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

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

  • 4

    我是使用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上出现旧复选框样式 .

  • 8

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

  • 2

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

    Step 1
    扩展ActionBarActivity

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

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

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

    colorAccent - 选中颜色
    android:textColorSecondary - 未经检查的颜色

    Step 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

  • 14

    1. Declare custom style in your styles.xml file.

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

    2. Apply this style to your RadioButton via android:theme attribute.

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

    100% Working

    只需为您的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" />
    
  • 4

    要指定颜色,请覆盖:

    检查颜色:

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

    对于不明显的颜色:

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

    使用复选框和单选按钮时,兼容性库会出现很多问题 .

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

    2)他们没有禁用状态(如果启用了所有复选框都无关紧要,否则你会有一个非常糟糕的惊喜) .

    请注意,默认的黑暗主题背景是灰色,而不是黑色,所以如果你保持默认,它就是“ok” .

    为了解决这个问题,我创建了以下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的开发者使用 .

  • 0

    像这样扩展 ActionBarActivity

    Public class MainActivity extends ActionBarActivity {
    
    //...
    
    }
    
  • 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>
    
  • 122

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

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

相关问题