简体   繁体   中英

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.

Here's a link to my styles.xml file . Sorry I had to censor a few things as this project is under an NDA.

I have tried many things including:

  • Changing android:background in windowTitleBackgroundStyle
  • Changing android:background in actionBarStyle
  • Changing android:background in actionBarWidgetStyle
  • Changing 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.

What am I missing or doing wrong?

Well you need to change android:background with windowBackground

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

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

create separate xml file for title on App.

that file contains like this..

In this custom layout, we can able to change the background color using color code..

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 :

            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. It works fine on newer devices but on my old 2.3 phone I was getting a gray background on the title.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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