首页 文章

如何使用XML更改ActionBarActivity的ActionBar的背景颜色?

提问于
浏览
254

细节:

我正在扩展ActionBarActivity .
自2011-11-06起,Eclipse和SDK已完全修补 .

<uses-sdk android:minSdkVersion="4" android:targetSdkVersion="14" />

使用Android 2.3.3部署到三星设备
申请已 android:theme="@android:style/Theme.Light"

Issue: 应用程序很轻,但ActionBar是蓝色的灰色图标,蓝色背景颜色几乎看不到 . 我也希望ActionBar很轻,所以它们的灰色图标更加明显 .

我试过修改样式但无济于事 .
我可能错过了一些微不足道的东西 .

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

16 回答

  • 497

    使用它,它会工作 .

    ActionBar actionbar = getActionBar();
    actionbar.setBackgroundDrawable(new ColorDrawable("color"));
    
  • 2
    getActionBar().setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.TabColor)));
    

    color.xml文件:

    <resources> <color name="TabColor">#11FF00</color> </resources>`
    
  • 38

    根据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)."

    因此,ActionBar不适用于API级别为10的目标环境(Android 2.3.3) .

    以防万一,如果您的目标是最低API等级11,您可以通过定义自定义样式来更改ActionBar的背景颜色,如下所示:

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

    并将“MyTheme”设置为应用程序/活动的主题 .

  • 15

    试试这个

    ActionBar bar = getActionBar();
    bar.setBackgroundDrawable(new ColorDrawable("COLOR"));
    
  • 54

    试试这个:

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

    使用此 - http://jgilfelt.github.io/android-actionbarstylegenerator/

    它是一个了不起的工具,可让您通过实时预览自定义操作栏 .

    我尝试了早期的答案,但总是遇到更改其他颜色的问题,如标签和字母,花了几个小时调整东西 . 这个工具帮助我在几分钟内完成了我的设计 .

    Here's a screenshot of the tool

    祝一切顺利! :)

  • 2

    我遇到了同样的问题,当我输入代码时,我的操作栏会变灰 . 机会是你原来的样式表看起来像这样:

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

    “DarkActionBar”让你的Action Bar保持灰色 . 我把它改成了这个,它起作用了:

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

    我还介绍了如何编辑文本颜色 . 此外,无需更改资源周围的任何内容 .

    -Warren

  • 69

    还可以在API <11中更改Actionbar的行为

    See the Android Official Documentation for reference

    我正在使用 minSdkVersion = "9"targetSdkVersion = "21" 构建一个应用程序我更改了操作栏的颜色并且它 works fine with API level 9

    这是一个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>
    

    并设置所需的操作栏的颜色

    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颜色也可以在 .class 文件中定义,代码片段是

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

    但这个 will not workAPI < 11 ,所以在 xml 中设置动作栏的样式只是API <11的方式

  • 117

    直接添加此行与您的颜色代码

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

    不需要每次都创建ActionBar对象......

  • 19

    在Nexus 4上,这似乎使颜色变得灰暗 .

    ActionBar bar = getActionBar(); // or MainActivity.getInstance().getActionBar()
    bar.setBackgroundDrawable(new ColorDrawable(0xff00DDED));
    bar.setDisplayShowTitleEnabled(false);  // required to force redraw, without, gray color
    bar.setDisplayShowTitleEnabled(true);
    

    (所有信用都归功于这篇文章,但它被隐藏在评论中,所以我想在这里表达一下)https://stackoverflow.com/a/17198657/1022454

  • 13

    尝试一行代码:

    getActionBar().setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.MainColor)));
    
  • -2

    使用下面的代码为actionbar文本和actionbar背景提供不同的颜色,只需在清单中使用下面的主题,对照你想要输出的活动:)

    <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>
    
  • 0

    这是您可以更改操作栏的颜色的方法 .

    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);
        }
    }
    }
    

    从您的MainActivity.java更改操作栏颜色,如下所示

    ActivityUtils.setActionBarColor(this, R.color.green_00c1c1);
    
  • 1

    此代码可能会有所帮助

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light">
        <!-- Customize your theme here. -->
    </style>
    <style name="Theme.MyTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- customize the color palette -->
        <item name="colorPrimary">@color/material_blue_500</item>
        <item name="colorPrimaryDark">@color/material_blue_700</item>
        <item name="colorAccent">@color/material_blue_500</item>
        <item name="colorControlNormal">@color/black</item>
    
    </style>
    <style name="CardViewStyle" parent="CardView.Light">
        <item name="android:state_pressed">@color/material_blue_700</item>
        <item name="android:state_focused">@color/material_blue_700</item>
        <!--<item name="android:background">?android:attr/selectableItemBackground</item>-->
    </style>
    
  • 5

    使用以下代码扩展活动时

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

    扩展AppCompatActivity时,请使用以下代码

    ActionBar actionbar = getSupportActionBar();
    actionbar.setBackgroundDrawable(new ColorDrawable("color"));
    
  • 198

    使用此代码..更改操作栏背景颜色 . 打开“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>
    

    注意:此代码仅适用于Android 3.0及更高版本

相关问题