如何在styles.xml中使用android:Theme.Material(Material theme)?

时间:2015-06-10 06:15:49

标签: android material-design android-appcompat android-theme android-styles

在我的应用程序中,我尝试将android:Theme.Material实现为样式values-21文件夹中的父主题:

 <!-- res/values-21/styles.xml -->
 <resources>
 <!-- your theme inherits from the material theme -->
 <style name="AppTheme" parent="android:Theme.Material">
    <!-- theme customizations -->
      <item name="android:colorPrimary">@color/primary</item>
    <item name="android:textColorPrimary">@color/text_primary</item>
    <!-- darker variant for the status bar and contextual app bars -->
    <item name="android:colorPrimaryDark">@color/primary_dark</item>
    <!-- theme UI controls like checkboxes and text fields -->
    <item name="android:colorAccent">@color/accent</item>
    <item name="android:navigationBarColor">@color/primary_dark</item>
   </style>
 </resources>

运行应用后,我的价格低于error

java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.

在values文件夹中。我有以下风格

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

但是,如果我在Theme.AppCompat.Light中添加相同的values-21 folder,那么它的工作正常。但是actionbar color并没有改变。

为什么我不能在values-21 folder中使用素材设计主题? 如何解决这个问题?

(注意:我的申请minsdk verison13maxsdk version22

  

我的活动扩展了AppCompactActivity

2 个答案:

答案 0 :(得分:7)

enter image description here

您的应用主题在清单文件中定义:

/res/values/styles.xml

您会在<!-- Base application theme. --> <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> <!-- Customize your theme here. --> <item name="colorPrimary">@color/colorPrimary</item> <item name="colorPrimaryDark">@color/colorPrimaryDark</item> <item name="colorAccent">@color/colorAccent</item> </style> 中找到此样式。

AppCompat

使用{{1}}即使对于早于Android 5.0的设备,也可以使用您的主题。主要的材料设计主题如下所示。

黑暗主题

enter image description here

灯光主题

enter image description here

带黑暗动作栏的灯光主题

enter image description here

进一步阅读

答案 1 :(得分:5)

如果您使用的是AppCompatActivity,只需使用res/values/styles.xml中的样式,并检查您用于colorPrimary,colorPrimaryDark的名称空间....

 <style name="AppTheme" parent="Theme.AppCompat.Light">
    <item name="colorPrimary">@color/primary</item>
    <item name="colorPrimaryDark">@color/primary_dark</item>
    <item name="colorAccent">@color/accent</item>
 </style>