سؤال

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 :

enter image description here

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

هل كانت مفيدة؟

المحلول

Try following code on 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...

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top