简体   繁体   中英

Android:Unable to set background color of Action Bar

I am trying to set background color of action bar which I am unable to do .

My styles.xml in values-v11 folder is as follows

<resources>

    <!--
        Base application theme for API 11+. This theme completely replaces
        AppBaseTheme from res/values/styles.xml on API 11+ devices.
    -->
    <style name="AppBaseTheme" parent="@android:style/Theme.Holo">


                <item name="android:actionBarStyle">@style/MyActionBar</item>

        <!-- API 11 theme customizations can go here. -->
    </style>

    <style name="MyActionBar">
        <item name="android:background">#99DD00</item>
    </style>
</resources>

The problem identified was that styles.xml (v-14) contained an empty tag with same name and it was picking up the theme from there

<style name="AppBaseTheme" >           
</style>

I am using the below in my application and it works perfectly:

<resources>

    <!-- Base application theme. -->
    <style name="CustomActionBar" parent="@android:style/Widget.Holo.Light.ActionBar">
        <item name="android:height">65dp</item>
        <item name="android:background">@android:color/black</item>
    </style>

    <style name="AppTheme" parent="android:Theme.Holo.Light.DarkActionBar">
        <item name="android:actionBarStyle">@style/CustomActionBar</item>    </style>
</resources>

For Android 3.0 and higher only

//res/values/themes.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- 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">@drawable/actionbar_background</item>
</style>
</resources>

For Android 2.1 and higher

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