简体   繁体   中英

How to change background color of Action Bar Title

It's easy to change the background color of the whole action bar, or the text color of only the action bar title but I've found no way to change the background color of only the title like this :

在此处输入图片说明

I've tried :

<style name="MyActionBar.TitleTextStyle" parent="@android:style/TextAppearance.Holo.Widget.ActionBar.Title">
    <item name="android:textColor">#000</item>
    <item name="android:padding">5px</item>
    <item name="android:background">#CC0000</item>
</style>

But only textColor works

尝试在onCreate()上执行以下代码

getActionBar().setBackgroundDrawable(new ColorDrawable(color.white));

First things first you need to have a custom theme declared for your application (or activity, depending on your needs). Something like…

<!-- Somewhere in AndroidManifest.xml -->
<application ... android:theme="@style/ThemeSelector">

Then, declare your custom theme for two cases, API versions with and without the Holo Themes. For the old themes we'll customize the windowTitleBackgroundStyle attribute, and for the newer ones the ActionBarStyle.

<style name="ThemeSelector" parent="android:Theme.Light">
    <item name="android:windowTitleBackgroundStyle">@style/WindowTitleBackground</item>
</style>

<style name="WindowTitleBackground">     
    <item name="android:background">@color/title_background</item>
</style>

<style name="ThemeSelector" parent="android:Theme.Holo.Light">
    <item name="android:actionBarStyle">@style/ActionBar</item>
</style>

<style name="ActionBar" parent="android:style/Widget.Holo.ActionBar">     
    <item name="android:background">@color/title_background</item>
</style>

That's it! Here we use @color/title_background as a background. It could also be a drawable, and you can also customize other attributes .

I created an ImageView where the Action Bar would have been by using 2 RelativeLayouts (0 padding on the outer, ordinary padding on the inner). Then I added the ImageView to the outer layout and manipulated it from there to act like an action bar, but with much more of my own control.

android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffffff"
tools:context="dayOfWeekApp.MainActivity"
>

<ImageView
    android:layout_width="match_parent"
    android:layout_height="100dp"
    android:id="@+id/topBorder"
    android:elevation="-1dp"
    android:background="#649175"
    android:layout_alignParentTop="true"
    android:layout_alignParentStart="true"/>

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    >

Other layout elements...

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