Action Bar color doesn't change with AppCompat

2019-08-05 22:42发布

问题:

Currently I'm trying to set a custom background color to a Action Bar on AppCompat v7:21. I've tried many possibilities and until now I can't figure out what's going on.

This is my res/values/styles.xml:

<resources>

    <color name="action_bar">#000</color>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light">
        <!-- Customize your theme here. -->
        <item name="actionBarStyle">@style/MyActionBar</item>

    </style>

    <!-- ActionBar styles -->
    <style name="MyActionBar" parent="Widget.AppCompat.Light.ActionBar">
        <item name="android:background">@color/action_bar</item>

    </style>

</resources>

回答1:

I've figured out! In fact, lot of UI things changed on API 21, and so on AppCompat v7. You can set Action Bar colors (among others) like this:

<!-- extend one of the Theme.AppCompat themes -->
<style name="Theme.MyTheme" parent="Theme.AppCompat.Light">
    <!-- customize the color palette -->
    <item name="colorPrimary">@color/material_blue_500</item>
    <item name="colorPrimaryDark">@color/material_blue_700</item>
    <item name="colorAccent">@color/material_green_A200</item>
</style>

Where the color* keys defines colors for related views, providing the visual consistency observed on Lollipop, i.e.: Is not possible (checked from my trial and error), set such colors individually when using AppCompat v7, like I was trying to do.

Obs.: I have tried using them, hours ago, on a <style> with parent="Widget.AppCompat.Light.ActionBar" which doesn't work. They must be placed within the "root" <style> element. It was this kind of mistake that make me write this.

Sorry for my English skills.