简体   繁体   中英

Changing background color of actionbar

My Android app uses a minimum SDK version 15. I am trying to change the background color of the Actionbar and want to do this by changing the style and theme. I've tried many things but the background remains dark. Here is what my styles look like:

<style name="AppTheme" parent="@style/Theme.AppCompat.Light.DarkActionBar">
    <item name="android:actionBarStyle">@style/MyActionBar</item>
</style>
<style name="MyActionBar" parent="@android:style/Widget.Holo.Light.ActionBar">
    <item name="android:background">#eeeeee</item>
</style>

In my manifext I set the application's theme to AppTheme.

By default AppCompat support library uses value of colorPrimary attribute as a color of ActionBar .

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="colorPrimary">#eeeeee</item>
</style>

But if you want to make color of ActionBar be independent from primary color of theme, then you can set it like this:

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="actionBarStyle">@style/ActionBar</item>
</style>

<style name="ActionBar" parent="Widget.AppCompat.ActionBar.Solid">
    <item name="background">@color/primary_teal_500</item>
</style>

Use colorPrimary and colorPrimaryDark

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->

        <item name="colorPrimary">@color/LightColor</item>
        <item name="colorPrimaryDark">@color/DarkColor</item>

    </style>

Modify your style.xml file as follows and try,

    <style name="AppTheme" parent="@style/Theme.AppCompat.Light.DarkActionBar">
       <item name="android:background">#eeeeee</item>
    </style>

And your menifest as follows,

android:theme="@style/AppTheme" >

When using the Support Library, define the action bar like this:

res/values/themes.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <!-- the theme applied to the application or activity -->
    <style name="CustomActionBarTheme"
           parent="@style/Theme.AppCompat.Light.DarkActionBar">
        <item name="android:actionBarStyle">@style/MyActionBar</item>

        <!-- Support library compatibility -->
        <item name="actionBarStyle">@style/MyActionBar</item>
    </style>

    <!-- ActionBar styles -->
    <style name="MyActionBar"
           parent="@style/Widget.AppCompat.Light.ActionBar.Solid.Inverse">
        <item name="android:background">@drawable/actionbar_background</item>

        <!-- Support library compatibility -->
        <item name="background">@drawable/actionbar_background</item>
    </style>
</resources>

Then apply your theme to your entire app or individual activities:

<application android:theme="@style/CustomActionBarTheme" ... />

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM