如何在styles.xmlandroid中使用android:Theme.Material(Material主题)?
How to use android:Theme.Material (Material theme) in styles.xml android?
在我的应用程序中,我试图将 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.
在值文件夹中。我有以下风格
<style name="AppTheme" parent="Theme.AppCompat.Light">
<!-- Customize your theme here. -->
</style>
但是,如果我在 values-21 folder
中添加相同的 Theme.AppCompat.Light
它工作正常。但是 actionbar color
没有改变。
为什么我不能在values-21 folder
中使用material设计主题?
如何解决这个问题?
(注意:我的申请 minsdk verison
是 13
而 maxsdk version
是 22
)
My activity extends AppCompactActivity
如果您使用的是 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>
您的应用主题已在清单文件中定义:
<application
android:theme="@style/AppTheme">
您会在 /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
允许您的主题甚至用于 Android 5.0 之前的设备。主要 Material 设计主题如下所示。
深色主题
浅色主题
带有深色操作栏的浅色主题
进一步阅读
- Using the Material Theme(Android 文档)
在我的应用程序中,我试图将 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.
在值文件夹中。我有以下风格
<style name="AppTheme" parent="Theme.AppCompat.Light">
<!-- Customize your theme here. -->
</style>
但是,如果我在 values-21 folder
中添加相同的 Theme.AppCompat.Light
它工作正常。但是 actionbar color
没有改变。
为什么我不能在values-21 folder
中使用material设计主题?
如何解决这个问题?
(注意:我的申请 minsdk verison
是 13
而 maxsdk version
是 22
)
My activity extends AppCompactActivity
如果您使用的是 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>
您的应用主题已在清单文件中定义:
<application
android:theme="@style/AppTheme">
您会在 /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
允许您的主题甚至用于 Android 5.0 之前的设备。主要 Material 设计主题如下所示。
深色主题
浅色主题
带有深色操作栏的浅色主题
进一步阅读
- Using the Material Theme(Android 文档)