【问题标题】:How to change background color of ActionBar title?如何更改 ActionBar 标题的背景颜色?
【发布时间】:2013-07-17 11:41:22
【问题描述】:

无论我如何修改样式,红色箭头指向的框的背景颜色都不会从背景改变。

如何更改该颜色?我的目标是 4.0+,不关心 4.0- 设备和操作系统。

这是my styles.xml file 的链接。抱歉,我不得不审查一些内容,因为该项目处于 NDA 之下。

我尝试了很多方法,包括:

  • windowTitleBackgroundStyle 中更改android:background
  • android:background 更改为actionBarStyle
  • actionBarWidgetStyle 中更改android:background
  • 更改android:actionBarItemBackground

这些都不起作用。该文本视图似乎始终坚持我的基本主题中的 android:background 值,并且没有任何东西能够覆盖它。

我错过了什么或做错了什么?

【问题讨论】:

标签: android android-actionbar android-styles


【解决方案1】:

你需要用 windowBackground 改变 android:background

<style name="CCCBaseTheme" parent="@android:style/Theme.Holo">
    <item name="android:windowBackground">@color/nav_background</item>
    <item name="android:textColor">@color/text</item>

    <item name="android:actionBarStyle">@style/CCCWidgetActionBar</item>

</style>

希望这会有所帮助。

【讨论】:

  • 哇,两年后哈哈。我不确定这是否是正确的解决方案,因为我无法再在问题的上下文中对其进行测试。如果其他人过来说这行得通,我会标记为已接受。
  • 呵呵昨天我遇到了这个问题我总是使用自定义操作栏:S ....我测试了它(windowBackground),它工作正常:)
【解决方案2】:

您的 xml 文件中有一些有趣的部分:

<!-- ActionBar -->
    <style name="CCCWidgetActionBar" parent="@android:style/Widget.Holo.ActionBar">
        <item name="android:background">@color/nav_background</item>
        <item name="android:textColor">@color/nav_text</item>
        <item name="android:titleTextStyle">@style/CCCWidgetActionBarTitle</item>
        <item name="android:textViewStyle">@style/CCCWidgetActionBarTitle</item>

        <item name="android:windowTitleStyle">@style/CCCWindowTitle</item>
        <item name="android:windowTitleBackgroundStyle">@style/CCCWindowTitleBackground</item>
        <item name="android:windowBackground">@color/nav_background</item>
    </style>

只是改变:

<item name="android:background">@color/nav_background</item>

<item name="android:background">@color/pink</item>

或其他任何颜色。

对我来说很好。

【讨论】:

  • 不适用于我的 Galaxy S II。与问题中所述相同的问题。
  • 标题文字不使用操作栏背景,只使用应用程序背景样式。这使得在不更改其他所有内容的背景颜色的情况下更改一种特定颜色是不可能的。
  • 我的联想 S880 也有同样的问题。我怀疑这可能是设备问题,但是 Google+ 应用在同一设备上的操作栏颜色很好.. 奇怪
  • 这不仅改变了ActionBar Title,还改变了整个bg!
【解决方案3】:

您可以自定义操作栏,并且可以根据需要更改标题背景颜色。

按照以下步骤操作:

只有这样才能为 Title 制作自定义 Sherlock 栏。

为 App 上的标题创建单独的 xml 文件。

该文件包含这样的内容..

在这个自定义布局中,我们可以使用颜色代码更改背景颜色..

header_sherlock.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:weightSum="3"
android:background="#008000"
>

     <TextView
    android:id="@+id/header_tvleft"
    android:layout_width="0dp"
    android:layout_height="fill_parent"
    android:layout_weight="1"
    android:gravity="left"
    android:layout_marginTop="@dimen/margintop"
    android:clickable="true"
    android:paddingLeft="@dimen/layoutpaddingleft"
    android:text="Textview"
    android:textColor="#ffffff" 
    android:textSize="@dimen/header_leftbtn"
     /> 


 <TextView
    android:id="@+id/header_tvcenter"
    android:layout_width="0dp"
    android:layout_height="fill_parent"
    android:layout_weight="1"
    android:gravity="center_horizontal"
    android:layout_marginTop="@dimen/margintop"
    android:layout_marginRight="@dimen/sherlockpaddingright"   
    android:layout_marginLeft="@dimen/sherlockpaddingleft"
    android:text="Textview"
    android:textColor="#ffffff"
    android:textStyle="bold"
    android:textSize="@dimen/header_title"
     />

    <TextView
    android:id="@+id/header_tvright"
    android:layout_width="0dp"
    android:layout_height="fill_parent"
    android:layout_weight="1"
    android:gravity="right"
    android:layout_marginTop="@dimen/margintop"
    android:clickable="true"
    android:paddingRight="@dimen/layoutpaddingright"
    android:text="Textview"
    android:textColor="#ffffff"
    android:textSize="@dimen/header_rightbtn"
     />

</LinearLayout>

对于 MainActivity 或您要添加标题的活动:

            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM); 
    getSupportActionBar().setCustomView(R.layout.header_sherlock);
    header_tvleft = (TextView) findViewById(R.id.header_tvleft);
    header_tvleft.setText("LeftTitleName");
    header_tvcenter = (TextView) findViewById(R.id.header_tvcenter);
    header_tvcenter.setText("CenterTitleName");
    header_tvright = (TextView) findViewById(R.id.header_tvright);
    header_tvright.setText("RightTitleName");

试试这个方法..

【讨论】:

  • 可以在不使用 ActionBarSherlock 的情况下做到这一点,而只使用 Android ActionBar?
【解决方案4】:

这是一个老问题,但我遇到了完全相同的问题 - 对我来说,问题是我有这个问题:

 <item name="android:actionBarItemBackground">@drawable/btn_dark</item>
 <item name="actionBarItemBackground">@drawable/btn_dark</item>

btn_dark 是可重复使用的可绘制对象,用于定义启用、按下和禁用状态的颜色的通用按钮。它在较新的设备上运行良好,但在我的旧 2.3 手机上,我的标题显示为灰色背景。

有趣的是,禁用状态(我已将其设置为深灰色)在旧设备上应用于操作栏标题的背景。将禁用的颜色设置为透明是解决方法。

【讨论】:

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