无法使用Material Design CheckBox来使用样式

时间:2015-08-11 11:33:34

标签: android checkbox material-design android-theme

我正在尝试将两个CheckBox视图添加到当前应用程序中的反馈片段,该应用片段使用AppCompat库(v7:22.2.1)。此应用的API范围是16-current(22)。我的清单设置了以下主题定义:

android:theme="@style/AppTheme"

在我的styles.xml文件中定义如下:

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Base.Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
    </style>

    <style name="AppTheme.FacebookFlatButton">
        <item name="android:background">@drawable/facebook_flat_button</item>
        <item name="android:textAppearance">?android:attr/textAppearanceMedium</item>
        <item name="android:textColor">@android:color/white</item>
    </style>

    <style name="AppTheme.TwitterFlatButton">
        <item name="android:background">@drawable/twitter_flat_button</item>
        <item name="android:textAppearance">?android:attr/textAppearanceMedium</item>
        <item name="android:textColor">@android:color/white</item>
    </style>

</resources>

问题是我的CheckBox(我还注意到我的EditText)视图没有默认为正确的颜色,以便在选中时可以查看它们。以下是Android Studio中预览和我的模拟器并排的屏幕截图:

Android Studio and Emulator

无论我在其他SO文章(例如this one)中使用此样式的样式项组合,模拟器或设备上的颜色都不会更改,但它们会在预览中更新。我也尝试过对我的自定义主题的父主题的各种调整,但似乎没什么关系。

我在Nexus 5(API 19)和Nexus 6(API 22)模拟器中尝试了这一点,它在我的Nexus 6设备上也做了同样的事情。

我在这里没有定义多少习惯,但我仍然觉得我在这里缺少一些小的关键信息。我错过了什么?

2 个答案:

答案 0 :(得分:2)

好的,经过大量的研究和测试,我有一个适用于CheckBox和EditText控件的解决方案。

我的(坏)假设是,一个主题可以照顾AppCompat和Material Design,但事实并非如此。基本上,对于AppCompat(&lt; API 21),您必须使用名称定义样式,而不使用 android:前缀,例如CheckBox和EditText:

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <!-- Base application theme -->
    <style name="AppTheme" parent="Theme.AppCompat.Light">
        <!-- Customize your theme here. -->
    </style> 

    <style name="AppTheme.EditText" parent="Widget.AppCompat.EditText">
        <item name="colorControlNormal">@color/almanac_red_dark</item>
        <item name="colorControlActivated">@color/almanac_red_light</item>
        <item name="colorControlHighlight">@color/almanac_red_light</item>
    </style>

    <style name="AppTheme.CheckBox">
        <item name="colorAccent">@color/almanac_red_dark</item>
    </style>

    <style name="AppTheme.FlatButton">
        <item name="android:textAppearance">?android:attr/textAppearanceMedium</item>
        <item name="android:textColor">@android:color/white</item>
    </style>

    <style name="AppTheme.FacebookFlatButton" parent="AppTheme.FlatButton">
        <item name="android:background">@drawable/facebook_flat_button</item>
    </style>

    <style name="AppTheme.TwitterFlatButton" parent="AppTheme.FlatButton">
        <item name="android:background">@drawable/twitter_flat_button</item>
    </style>

</resources>

但您必须在values-v21目录中使用Material Design版本(&gt; API 21)的另一个版本的样式(使用Material Design父主题),使用 { {1}}前缀:

android:

我已经尝试过Nexus 4(API 16),Nexus 5(API 19)和Nexus 6(API 22)模拟器以及我的物理Nexus 6设备,所有内容都看起来像我期望的那样。

答案 1 :(得分:1)

你需要指定它的亮色,放在你的主风格'AppTheme'中的下面

  <item name="colorAccent">@color/color_primary</item>

这将更改您的复选框和编辑文字颜色