简体   繁体   English

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

[英]How to use android:Theme.Material (Material theme) in styles.xml android?

In my application, I am trying to implement android:Theme.Material as a parent theme in styles values-21 folder: 在我的应用程序中,我试图在styles values-21文件夹中实现android:Theme.Material作为父主题:

 <!-- 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>

After running the app, I am getting below error 运行应用程序后,我收到以下error

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

In values folder. 在values文件夹中。 I have below style 我有以下风格

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

But, if I add the same Theme.AppCompat.Light in values-21 folder its working fine. 但是,如果我在values-21 folder添加相同的Theme.AppCompat.Light ,它的工作正常。 but actionbar color is not changing. 但是actionbar color没有变化。

Why can't i use the material design theme in values-21 folder ? 为什么我不能在values-21 folder使用材质设计主题? How to solve this problem? 如何解决这个问题呢?

(note: my application minsdk verison is 13 and maxsdk version is 22 ) (注意:我的申请minsdk verison13maxsdk version22

My activity extends AppCompactActivity 我的活动扩展了AppCompactActivity

在此输入图像描述

Your app theme is defined in the manifest file: 您的应用主题在清单文件中定义:

<application
    android:theme="@style/AppTheme">

You will find this style defined in /res/values/styles.xml . 您将在/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>

Using AppCompat allows your themes to be used even for devices older than Android 5.0. 使用AppCompat可以让您的主题甚至用于早于Android 5.0的设备。 The main Material Design Themes are shown below. 主要材料设计主题如下所示。

Dark Theme 黑暗主题

在此输入图像描述

Light Theme 光主题

在此输入图像描述

Light Theme with Dark Action Bar 浅色主题与黑暗动作栏

在此输入图像描述

Further Reading 进一步阅读

If your are using an AppCompatActivity , just use only a style in your res/values/styles.xml , and check the namespace that your are using for colorPrimary, colorPrimaryDark.... 如果您正在使用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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM