简体   繁体   English

如何更改ActionBar标题的背景颜色?

[英]How to change background color of ActionBar title?

No matter how I modify the styles, the background color of the box pointed to by the red arrow just won't change from the background. 无论我如何修改样式,红色箭头指向的框的背景颜色都不会从背景中改变。

IMG

How do I change that color? 我该如何改变颜色? I'm targeting 4.0+ and don't care about 4.0- devices and OSes. 我的目标是4.0+而不关心4.0-设备和操作系统。

Here's a link to my styles.xml file . 这是我的styles.xml文件的链接。 Sorry I had to censor a few things as this project is under an NDA. 对不起,由于这个项目属于NDA,我不得不审查一些事情。

I have tried many things including: 我尝试了很多东西,包括:

  • Changing android:background in windowTitleBackgroundStyle windowTitleBackgroundStyle更改android:background
  • Changing android:background in actionBarStyle actionBarStyle更改android:background
  • Changing android:background in actionBarWidgetStyle actionBarWidgetStyle更改android:background
  • Changing android:actionBarItemBackground 改变android:actionBarItemBackground

None of those work. 这些都不起作用。 That text view seems to always stick to the android:background value from my base theme and nothing was able to override it. 该文本视图似乎始终坚持我的基本主题的android:background值,没有任何东西可以覆盖它。

What am I missing or doing wrong? 我错过了什么或做错了什么?

Well you need to change android:background with windowBackground 那你需要用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>

hope this will help. 希望这会有所帮助。

There is the interisting part of your xml file : 你的xml文件有interint部分:

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

Just change : 只是改变 :

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

By 通过

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

Or any color else. 或任何其他颜色。

Works fine for me. 对我来说很好。

you make custom Action-bar and possible to change the Title Background Color, whatever you want. 您可以自定义操作栏,并可以根据需要更改标题背景颜色。

Follow this Steps: 请遵循以下步骤:

Only possible for this to make Custom Sherlock bar for Title. 只有这样才能使Custom Sherlock成为Title。

create separate xml file for title on App. 在App上为标题创建单独的xml文件。

that file contains like this.. 该文件包含这样的..

In this custom layout, we can able to change the background color using color code.. 在这个自定义布局中,我们可以使用颜色代码更改背景颜色。

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

For MainActivity or which activity you want to add title : 对于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");

Try this Method.. 试试这个方法..

This is an old question but I've just had exactly the same problem - for me the issue was that I had this: 这是一个古老的问题,但我只是有完全相同的问题 - 对我来说问题是我有这个问题:

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

Where btn_dark was a re-used drawable for generic buttons defining a color for the enabled, pressed and disabled states. 其中btn_dark是一个重复使用的drawable,用于定义启用,按下和禁用状态颜色的通用按钮。 It works fine on newer devices but on my old 2.3 phone I was getting a gray background on the title. 它在较新的设备上工作正常,但在我的旧2.3手机上我得到了标题的灰色背景。

Interestingly the disabled state (for which I had set as a dark gray color), was applied on older devices to the background of the action bar title. 有趣的是,禁用状态(我将其设置为深灰色)在旧设备上应用于操作栏标题的背景。 Setting the disabled color to transparent was the fix. 将禁用的颜色设置为透明是修复。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM