简体   繁体   中英

Android ActionBar not changing background color

I am trying to change the background color of my action bar but it doesnt change. My app is using the AppCompat theme if that affects anything.

Here is my theme that is being used. My app points to use this theme so that is not the problem

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

<!-- Action Bar Styles -->
<style name="ActionBar" parent="Widget.AppCompat.ActionBar">
    <item name="android:background">@color/blue</item>
    <item name="background">@color/blue</item>
</style>

In your ActionBar style, delete the android:background, only keep the background attribute. Also make sure to implement your AppTheme in the application tag or in the activities extending the action bar in your AndroidManifest.xml file. I've also added the "@style/" attribute before Widget.AppCompat since its a default style defined in the android library.

Your code should look like this :

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

<!-- Action Bar Styles -->
<style name="ActionBar" parent="@style/Widget.AppCompat.ActionBar">
    <item name="background">#0000ff</item>
</style>

You can se this from code as well

ab.setBackgroundDrawable(new ColorDrawable(newcolor));

Just in case your xml is not working

According to this link, https://developer.android.com/reference/android/R.attr.html#background it doesn't seem to take color resource, so you either give it a hex color value or create a drawable that uses the color you want. You could try the following alternative to see if it works.

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

<!-- Action Bar Styles -->
<style name="ActionBar" parent="Widget.AppCompat.ActionBar">
    <item name="android:background">#0000ff</item>
    <item name="background">#0000ff</item>
</style>

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