API 21下的主要深色android
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26444391/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
primary dark color android under API 21
提问by Nick
i want to color the Statusbar, for a better look in Android 5.0. I want to set the three color primarydark,light and accent.
我想为状态栏着色,以便在 Android 5.0 中更好看。我想设置三种颜色primarydark,light和accent。
But my minimum API is 15. Is there any chance just to use this just under API lvl 21? Or do I have to create a seperate app with min. sdk 21?
但是我的最小 API 是 15。有没有机会在 API lvl 21 下使用它?或者我必须用 min. 创建一个单独的应用程序吗?sdk 21?
EDIT: Now i get everything i needed but the statisbar color won't change.
编辑:现在我得到了我需要的一切,但 statisbar 颜色不会改变。
This is my values-v21/styles.xml
这是我的 values-v21/styles.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="AppBaseTheme" parent="android:Theme.Material.Light">
<!-- API 21 theme customizations can go here. -->
<!-- Main theme colors -->
<!-- your app branding color for the app bar -->
<item name="android:colorPrimary">@color/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>
</style>
</resources>
And this the normal style.xml
这是正常的 style.xml
<resources>
<!--
Base application theme, dependent on API level. This theme is replaced
by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
-->
<style name="AppBaseTheme" parent="android:Theme.Light">
<!--
Theme customizations available in newer API levels can go in
res/values-vXX/styles.xml, while customizations related to
backward-compatibility can go here.
-->
</style>
<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
<!-- All customizations that are NOT specific to a particular API-level can go here. -->
</style>
<!-- the theme applied to the application or activity -->
<style name="CustomActionBarTheme"
parent="@android:style/Theme.Holo.Light.DarkActionBar">
<item name="android:actionBarStyle">@style/MyActionBar</item>
</style>
<!-- ActionBar styles -->
<style name="MyActionBar"
parent="@android:style/Widget.Holo.Light.ActionBar.Solid.Inverse">
<item name="android:background">@color/blue</item>
</style>
</resources>
Any Ideas why this won't work?
任何想法为什么这不起作用?
回答by ipavl
You can have different styles for different API levels.
您可以为不同的 API 级别使用不同的样式。
For API < 21, you would use a normal res/values/styles.xml
, and then for API 21+, you would have res/values-v21/styles.xml
.
对于 API < 21,您将使用普通res/values/styles.xml
,然后对于 API 21+,您将使用res/values-v21/styles.xml
.
If the device is running API 21 or higher, then it will use the file in the -v21
folder. If you just want to set <color>
values, then just name the keys the same and do the same with colors.xml
.
如果设备运行 API 21 或更高版本,则它将使用文件-v21
夹中的文件。如果您只想设置<color>
值,那么只需将键命名相同并使用colors.xml
.
http://developer.android.com/guide/topics/resources/providing-resources.html#AlternativeResources
http://developer.android.com/guide/topics/resources/providing-resources.html#AlternativeResources
Example:
例子:
res/values/colors.xml
:
res/values/colors.xml
:
<!-- Colours for API <21 -->
<color name="primaryDark">#800000</color>
<color name="light">#800080</color>
<color name="accent">#FF0000</color>
res/values-v21/colors.xml
:
res/values-v21/colors.xml
:
<!-- Colours for API 21+ -->
<color name="primaryDark">#000008</color>
<color name="light">#080008</color>
<color name="accent">#0000FF</color>
回答by szynka
try set background programatically:
尝试以编程方式设置背景:
actionBar.setBackgroundDrawable(getResources().getDrawable(R.drawable.actionbar_background));
actionbar_background.xml
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<solid android:color="#e59300" />
</shape>
</item>
<item android:bottom="3dp">
<shape>
<solid android:color="#fbb903" />
</shape>
</item>
</layer-list>
回答by li2
colorPrimaryDark
is used to color the status bar...Status bar theme is a feature that was added to Android in Lollipop. Keep in mind that the status bar will be black on older devices (no matter what the theme specifies).
colorPrimaryDark
用于为状态栏着色...状态栏主题是在 Lollipop 中添加到 Android 的一项功能。请记住,旧设备上的状态栏将是黑色的(无论主题指定什么)。
Referenced from 《Android Programming Guide 2ed》the big nerd ranch guide P360. But I cannot find it in android developer guide.
引用自《Android Programming Guide 2ed》大书呆子牧场指南P360。但我在 android 开发者指南中找不到它。