How do I change the background color of the ActionBar of an ActionBarActivity using XML?

后端 未结 20 2700
我寻月下人不归
我寻月下人不归 2020-11-22 01:09

Details:

I\'m extending ActionBarActivity.
Eclipse and SDK fully patched as of 2011-11-06.



        
相关标签:
20条回答
  • 2020-11-22 01:45

    Behavior of Actionbar can also be changed in APIs < 11

    See the Android Official Documentation for reference

    I am building an app with minSdkVersion = "9" and targetSdkVersion = "21" I changed the color of action bar and it works fine with API level 9

    Here is an xml

    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">@color/actionbar_background</item>
    
            <!-- Support library compatibility -->
            <item name="background">@color/actionbar_background</item>
        </style>
    </resources>
    

    and set the color of actionbar you want

    res/values/colors.xml

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <color name="actionbar_background">#fff</color> //write the color you want here
    </resources>
    

    Actionbar color can also be defined in .class file, the snippet is

    ActionBar bar = getActionBar();
    bar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#0000ff")));
    

    but this will not work with the API < 11, so styling the actionbar in xml is only way for API < 11

    0 讨论(0)
  • 2020-11-22 01:45

    If you are using androidx AppCompact. Use below code.

    androidx.appcompat.app.ActionBar actionBar = getSupportActionBar();
    actionBar.setBackgroundDrawable(new ColorDrawable("Color"));
    
    0 讨论(0)
  • 2020-11-22 01:46

    I had the same problem, where my Action Bar would turn grey when I entered that code. Chances are your original style sheet looked like this:

    <style name="AppBaseTheme" parent="android:Theme.Holo.Light.DarkActionBar">
        <!-- API 14 theme customizations can go here. -->
    </style>
    

    The "DarkActionBar" was what was keeping your Action Bar grey. I changed it to this, and it worked:

    <style name="AppBaseTheme" parent="android:Theme.Holo.Light">
        <!-- API 14 theme customizations can go here. -->
        <item name="android:actionBarStyle">@style/MyActionBar</item>
    </style>
    
    <style name="MyActionBar" parent="@android:style/Widget.Holo.Light.ActionBar">
        <item name="android:background">#2aa4cd</item>
        <item name="android:titleTextStyle">@style/Theme.MyAppTheme.ActionBar.TitleTextStyle</item>
    </style>        
    
    <style name="Theme.MyAppTheme.ActionBar.TitleTextStyle" parent="android:style/TextAppearance.Holo.Widget.ActionBar.Title">
        <item name="android:textColor">#FFFFFF</item>
    </style>    
    

    I also threw in how to edit the text color. Also, no need to change anything surrounding the resources.

    -Warren

    0 讨论(0)
  • 2020-11-22 01:50

    As per documentation - "You can control the behaviors and visibility of the action bar with the ActionBar APIs, which were added in Android 3.0 (API level 11)."

    So, ActionBar will not work for your target environment which is at API level 10 (Android 2.3.3).

    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.

    0 讨论(0)
  • 2020-11-22 01:52

    This is how you can change the color of Action Bar.

    import android.app.Activity;
    import android.content.Context;
    import android.graphics.Color;
    import android.graphics.drawable.ColorDrawable;
    import android.os.Build;
    import android.support.v4.content.ContextCompat;
    import android.support.v7.app.ActionBar;
    import android.support.v7.app.AppCompatActivity;
    
    
    public class ActivityUtils {
    
    public static void setActionBarColor(AppCompatActivity appCompatActivity, int colorId){
        ActionBar actionBar = appCompatActivity.getSupportActionBar();
        ColorDrawable colorDrawable = new ColorDrawable(getColor(appCompatActivity, colorId));
        actionBar.setBackgroundDrawable(colorDrawable);
    }
    
    public static final int getColor(Context context, int id) {
        final int version = Build.VERSION.SDK_INT;
        if (version >= 23) {
            return ContextCompat.getColor(context, id);
        } else {
            return context.getResources().getColor(id);
        }
    }
    }
    

    From your MainActivity.java change the action bar color like this

        ActivityUtils.setActionBarColor(this, R.color.green_00c1c1);
    
    0 讨论(0)
  • 2020-11-22 01:52

    Sometimes this option throws NullPointerException

    ActionBar actionbar = getActionBar(); actionbar.setBackgroundDrawable(new ColorDrawable("color"));

    But this option is worked for me. So you can try this.

    getSupportActionBar().setBackgroundDrawable(new ColorDrawable(Color.parseColor("#FFFFFF")));
    

    Happy Coding

    0 讨论(0)
提交回复
热议问题