primary dark color android under API 21

2019-02-15 09:35发布

问题:

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.

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?

EDIT: Now i get everything i needed but the statisbar color won't change.

This is my 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

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

回答1:

You can have different styles for different API levels.

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.

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.

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

Example:

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>


回答2:

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>


回答3:

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).

Referenced from 《Android Programming Guide 2ed》the big nerd ranch guide P360. But I cannot find it in android developer guide.