API 21 以下版本的 Android 的主要暗色调颜色

3
我希望为Android 5.0上的状态栏着色,以使其外观更好看。我想设置三种颜色:primarydark、light和accent。
但我的最低API是15。是否有可能在API级别21以下仅使用此功能?或者我必须创建一个具有最小sdk 21的单独应用程序?
编辑: 现在我已经得到了我需要的一切,但状态栏颜色不会改变。
这是我的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>

这是普通的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>

任何想法为什么这行代码不起作用?
3个回答

8
你可以针对不同的API级别使用不同的样式。
对于API<21,您需要使用普通的res/values/styles.xml,然后对于API 21+,您需要有res/values-v21/styles.xml
如果设备正在运行API 21或更高版本,则它将使用-v21文件夹中的文件。如果您只想设置<color>值,则只需将键命名相同,并在colors.xml中执行相同操作。

http://developer.android.com/guide/topics/resources/providing-resources.html#AlternativeResources

例子:

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:

<!-- Colours for API 21+ -->
<color name="primaryDark">#000008</color>
<color name="light">#080008</color>
<color name="accent">#0000FF</color>

谢谢!找不到相关信息。 - Nick
添加了我的XML文件。对我来说不起作用。仍然有黑色状态栏。 - Nick
你是否也指定了不同的 colors.xml 文件? - ipavl
刚刚创建了主题。我在哪里可以指定它们? - Nick
你需要在styles.xml文件中使用它们,就像你在问题中所做的那样。确保它们的名称相同,因为我的示例和你的问题具有不同的命名。 - ipavl
OP 正在尝试更改状态栏颜色。在 Lollipop 之前,状态栏颜色是不可调的。虽然您提出的解决方案将更改与这些资源相关联的值,但它们不会用于样式化 Lollipop 之前的状态栏。 - tir38

2

尝试通过编程设置背景:

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>

2

colorPrimaryDark 是用来设置状态栏颜色的... 状态栏主题是 Lollipop 版本中增加的功能。 请注意,在较旧的设备上状态栏将始终为黑色(无论主题如何指定)。

引自《Android Programming Guide 2ed》大牛书 P360,但我在 Android 开发者指南中找不到它。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接