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

Joh*_*ohn 14 android android-appcompat android-theme android-styles material-design

在我的应用程序中,我试图android:Theme.Material在styles 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>
Run Code Online (Sandbox Code Playgroud)

运行应用程序后,我到了下面 error

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

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

<style name="AppTheme" parent="Theme.AppCompat.Light">
    <!-- Customize your theme here. -->
</style>
Run Code Online (Sandbox Code Playgroud)

但是,如果我Theme.AppCompat.Lightvalues-21 folder其工作正常情况下添加相同的内容.但actionbar color并没有改变.

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

(注:我的应用程序minsdk verison13maxsdk version22)

我的活动扩展了AppCompactActivity

Sur*_*gch 7

在此输入图像描述

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

<application
    android:theme="@style/AppTheme">
Run Code Online (Sandbox Code Playgroud)

你会发现这种风格定义/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>
Run Code Online (Sandbox Code Playgroud)

使用AppCompat允许您的主题甚至用于早于Android 5.0的设备.主要材料设计主题如下所示.

黑暗主题

在此输入图像描述

光主题

在此输入图像描述

浅色主题与黑暗动作栏

在此输入图像描述

进一步阅读


Gab*_*tti 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>
Run Code Online (Sandbox Code Playgroud)