【问题标题】:How do I change the background color of the ActionBar of an ActionBarActivity using XML?如何使用 XML 更改 ActionBarActivity 的 ActionBar 的背景颜色?
【发布时间】:2011-12-22 21:46:48
【问题描述】:

详情:

我正在扩展 ActionBarActivity。
自 2011 年 11 月 6 日起,Eclipse 和 SDK 已完全修补。

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

部署到装有 Android 2.3.3 的三星设备
申请有android:theme="@android:style/Theme.Light"

问题: 应用程序很轻,但 ActionBar 是蓝色的,带有灰色图标,在蓝色背景色中几乎看不到。我还希望 ActionBar 轻一点,这样它们的灰色图标更显眼。

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

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

【问题讨论】:

  • 关闭这个老栗子。根据大众的需求,指向 Sannidhi,但是对这个问题的回答跨越了相当多的 Android 版本,在我的上下文中,David Millers 建议使用 SherlockActionBar 是最相关的。
  • 其他人的小花絮:如果您将 XML 视图中的第一个元素设置为与 ActionBar 相同的背景(在我们的示例中为白色),则 ActionBar 会变为灰色。
  • 仔细想想,TitleBar 和 ActionBar 并没有提供什么特别之处。它们更像是一种讨厌的东西,尤其是当你不认识它们的时候。 A.更好的选择是隐藏它们并设计自己的标题栏。您可以使用 Views 和 drawables 添加自己的小部件。由于标题栏不提供小部件。对于所有的菜单项,操作栏只能有一个下拉列表。甚至很多应用都放弃了操作栏,转而使用自己的。

标签: android android-actionbar android-theme android-styles


【解决方案1】:

根据documentation - “您可以使用 Android 3.0(API 级别 11)中添加的 ActionBar API 来控制操作栏的行为和可见性。”

因此,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”设置为应用程序/活动的主题。

【讨论】:

  • @AlexanderFragotsis 使用&lt;item name="android:textColor"&gt;@color/my_color&lt;/item&gt;。
  • 如果您使用 ActionBarCompat 库,则应使用相同的方法。唯一的区别是您应该更改 actionBarStyle(不带 android: 前缀)项来定义 actionbar 在 3.0 之前的 Android 设备上的外观
  • 有没有可能,而不是放置颜色的 HEX 代码,而是引用 @android:color 甚至是在 colors.xml 中定义的自定义颜色?我试过了,没有成功。
  • 我无法使用“@color/my_color”更改文本操作栏文本颜色
  • 这对我来说不适用于 android:background 标签,但是当我省略了 android 标签并只放置 ​​ 时,所有平台的颜色都会改变。
【解决方案2】:

试试这个

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

【讨论】:

  • 11-06 10:53:16.985: E/AndroidRuntime(16956): java.lang.NoSuchMethodError: com.acme.myActivity.getActionBar
  • @coder_For_Life22 你知道我怎样才能获得上下文操作栏吗?
  • afaik,支持库本身没有 ActionBar,因为您必须包含 SherlockActionBar。
  • 也许这很奇怪,但在我的情况下 bar.setBackgroundDrawable(new ColorDrawable(Color.BLUE)); 将操作栏颜色更改为灰色。如果放置其他颜色也会发生这种情况,例如Color.RED、Color.GREEN 等等……有人有同样的问题,或者知道这个问题的原因吗?我使用的是 Nexus S 设备
  • stackoverflow.com/a/17198657/1022454 这篇文章似乎解决了操作栏颜色变灰的问题。
【解决方案3】:

试试这个:

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

【讨论】:

  • 这如何融入其余代码?只是好奇在哪里实际添加这个
  • 我的确切问题是@JGallardo。你在哪里添加这个?我在onCreate 中尝试过,但它给出了NullPointerException 编辑:这是我添加到restoreActionBar ActionBar actionBar = getSupportActionBar(); 的代码actionBar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#E5E4E2")));
  • 将它添加到您的 Activity 的 onCreate 方法中。
  • 使用 getSupportActionBar() 代替 getActionBar()
【解决方案4】:

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

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

我尝试了较早的答案,但在更改标签和字母等其他颜色时总是遇到问题,并且花了数小时调整内容。这个工具帮助我在几分钟内完成了我的设计。

一切顺利! :)

【讨论】:

  • 如何设置我的应用程序在将文件添加到我的应用程序后使用它们?
  • 在上面截图的上下文中(命名示例),粘贴生成的 res 文件夹后,你应该有一个新的样式@style/Theme.Example。在 AndroidManifest.xml 中,设置 。应该工作:)
  • 已弃用。不幸的是
【解决方案5】:

我遇到了同样的问题,当我输入该代码时,我的操作栏会变成灰色。您的原始样式表可能是这样的:

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

“DarkActionBar”是让您的操作栏保持灰色的原因。我把它改成了这个,它起作用了:

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

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

-沃伦

【讨论】:

    【解决方案6】:

    也可以在 API 中更改 Actionbar 的行为

    参考Android Official Documentation

    我正在使用 minSdkVersion = "9" 和 targetSdkVersion = "21" 构建一个应用程序,我更改了操作栏的颜色,它在 API 级别 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文件中定义,sn -p是

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

    但是这个无法使用 API ,所以在xml 中设置操作栏的样式是API 的唯一方法

    【讨论】:

    • 在片段中使用时,sn-p 在我的应用程序中不起作用。我将其更改为 ActionBar bar = getActivity().getActionBar(); .一旦片段即将打开,它就会崩溃。
    • 发现了问题,它适用于:ActionBar actionBar = getSupportActionBar();
    【解决方案7】:

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

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

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

    【讨论】:

    • 最好使用资源来获取颜色,而不是将其作为文本。 getSupportActionBar().setBackgroundDrawable(new ColorDrawable(getResources().getColor(android.R.color.black)));
    【解决方案8】:

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

    (这篇文章的所有功劳,但它被埋在了 cmets 中,所以我想在这里浮出水面) https://stackoverflow.com/a/17198657/1022454

    【讨论】:

    • 在花了无数小时试图找出 WTH 是错误的之后,这个答案帮助了我。
    【解决方案9】:

    尝试一行代码:

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

    【讨论】:

    • 我的应用程序因这个答案而崩溃。为了使它在片段中工作,我将其更改为: getActivity().getActionBar().setBackgroundDrawable(new ColorDrawable(getActivity().getResources().getColor(R.color.col_nar))); ,也试过 getActivity().getActionBar().setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.col_nar))); .一旦打开片段,它就会崩溃。
    • 发现了问题,它适用于:ActionBar actionBar = getSupportActionBar();
    • 答案是活动。如果你想将它与片段一起使用,那么你必须将 getActivity().getActionBar() 用于片段
    【解决方案10】:

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

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

    【讨论】:

      【解决方案11】:

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

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

      【讨论】:

        【解决方案12】:

        当您扩展 Activity 时,请使用以下代码

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

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

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

        【讨论】:

          【解决方案13】:

          这对我有用,适用于 AppCompatActivity,

              <item name="actionBarStyle">@style/BlackActionBar</item>
          </style>
          
          <style name="BlackActionBar" parent="@style/Widget.AppCompat.ActionBar.Solid">
              <item name="background">@android:color/black</item>
              <item name="titleTextStyle">@style/BlackActionBarTitleTextStyle</item>
          </style>
          
          <style name="BlackActionBarTitleTextStyle" parent="@style/TextAppearance.AppCompat.Widget.ActionBar.Title">
              <item name="android:textColor">@android:color/white</item>
              <item name="android:fontFamily">@font/cinzel_decorative</item>
          </style>
          

          【讨论】:

            【解决方案14】:

            2021:没有弃用的 Kotlin oneliner:

            supportActionBar?.setBackgroundDrawable(ColorDrawable(ContextCompat.getColor(this,R.color.red)))
            

            简单的放到onCreate中

            【讨论】:

              【解决方案15】:

              此代码可能会有所帮助

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

              【讨论】:

                【解决方案16】:

                有时此选项会抛出 NullPointerException

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

                但是这个选项对我有用。所以你可以试试这个。

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

                快乐编码

                【讨论】:

                  【解决方案17】:

                  如果您使用的是 androidx AppCompact。使用下面的代码。

                  androidx.appcompat.app.ActionBar actionBar = getSupportActionBar();
                  actionBar.setBackgroundDrawable(new ColorDrawable("Color"));
                  

                  【讨论】:

                    【解决方案18】:

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

                    【讨论】:

                      【解决方案19】:

                      对于那些使用 API 21 或更高版本的人来说非常简单使用下面的代码

                      用于深色操作栏

                      <resources>
                      <style name="AppTheme" parent="Theme.AppCompat.NoActionBar">
                          <!-- Customize your theme here. -->
                          <item name="android:background">@color/colorDarkGrey</item>
                      </style>
                      

                      for_LightActionBar

                      <resources>
                      <style name="AppTheme" parent="Theme.AppCompat.Light.ActionBar">
                          <item name="android:background">@color/colorDarkGrey</item>
                      </style>
                      

                      【讨论】:

                        【解决方案20】:

                        使用这个,它会工作。

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

                        【讨论】:

                          【解决方案21】:
                          getActionBar().setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.TabColor)));
                          

                          color.xml 文件:

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

                          【讨论】:

                            猜你喜欢
                            • 2015-08-14
                            • 1970-01-01
                            • 1970-01-01
                            • 2013-03-03
                            • 2015-07-25
                            • 1970-01-01
                            • 1970-01-01
                            • 2014-03-05
                            • 1970-01-01
                            相关资源
                            最近更新 更多