简体   繁体   中英

How to change the color of the actionbar?

I've been trying to change the actionbar's color without any succes. I've been searching for a while now... I tried the solution of this post: How to change ActionBar color?

This is my styles.xml:

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

<style name="ActionBar" parent="Widget.AppCompat.Light.ActionBar.Solid.Inverse">
    <item name="android:background">#0091ea</item>
</style>
</resources>

You can do it by this way

  <style name="MyTheme" parent="@android:style/Theme.Holo.Light">
        <item name="android:actionBarStyle">@style/MyActionBar</item>
    </style>

    <style name="MyActionBar" parent="@android:style/Widget.Holo.Light.ActionBar">
        <item name="android:titleTextStyle">@style/TitleBarTextColor</item>
        <item name="android:background">YOUR_COLOR_CODE</item>
    </style>

    <style name="TitleBarTextColor" parent="@style/TextAppearance.AppCompat.Widget.ActionBar.Title">
        <item name="android:textColor">YOUR_COLOR_CODE</item>
    </style>

Obivously, you have to add this theme on your Activity in your AndroidManifest.

EDIT

Add those imports on your Activity

 import android.app.ActionBar;
 import android.graphics.drawable.ColorDrawable;

Just below on yout onCreate put this

ActionBar ab = getActionBar(); 
       ColorDrawable colorDrawable = new ColorDrawable(Color.parseColor("#81a3d0"));     
       ab.setBackgroundDrawable(colorDrawable);

This works for me :

getActionBar().setBackgroundDrawable(new ColorDrawable(0xff1d97dd));

Let me know if it works :)

You can use the Android Asset Studio to create a custom ActionBar style, and therefore choose what color you want your AB to have.

You could also try the solution from this StackOverflow post How do I change the background color of the ActionBar of an ActionBarActivity using XML?

Quote:

Just in case, if you target for minimum API level 11 , you can change ActionBar's background color by defining custom style, as:

 <resources> <style name="MyTheme" parent="@android:style/Theme.Holo.Light"> <item name="android:actionBarStyle">@style/MyActionBar</item> </style> <style name="MyActionBar" parent="@android:style/Widget.Holo.Light.ActionBar"> <item name="android:background">ANY_HEX_COLOR_CODE</item> </style> </resources>

And, set "MyTheme" as theme for application / activity.

To change the action bar background, create a custom theme for your activity that overrides the actionBarStyle property. This property points to another style in which you can override the background property to specify a drawable resource for the action bar background.

When supporting Android 3.0 and higher only, you can define the action bar's background 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="@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>

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

AndroidManifest.xml

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

Source

You can set custom colors in your styles.xml .

<resources>
  <!-- inherit from the material theme -->
  <style name="AppTheme" parent="android:Theme.Material">
    <!-- 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>

Then create a colors.xml file and set your colors there in hex format.

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="primary">#F44336</color>
    <color name="primaryDark">#D32F2F</color>
    <color name="accent">#448AFF</color>
</resources>

Go here to learn more: https://developer.android.com/training/material/theme.html#ColorPalette

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