如何更改Action Bar Title的背景颜色

时间:2014-05-08 11:32:13

标签: android android-actionbar

可以轻松更改整个操作栏的背景颜色,或仅更改操作栏标题的文字颜色,但我发现无法更改此标题的背景颜色,如此:

enter image description here

我试过了:

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

但只有textColor有效

3 个答案:

答案 0 :(得分:4)

onCreate()

上尝试以下代码
getActionBar().setBackgroundDrawable(new ColorDrawable(color.white));

答案 1 :(得分:1)

首先,您需要为您的应用程序(或活动,根据您的需要)声明一个自定义主题。有点像...

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

然后,为两种情况声明您的自定义主题,包括和不包含Holo主题的API版本。对于旧主题,我们将自定义windowTitleBackgroundStyle属性,对于较新的主题,我们将自定义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>

而已!这里我们使用@ color / title_background作为背景。它也可以是一个可绘制的,你也可以自定义其他属性。

答案 2 :(得分:0)

我创建了一个ImageView,其中Action Bar将使用2个RelativeLayouts(内部的外部普通填充为0填充)。然后我将ImageView添加到外部布局并从那里进行操作,就像操作栏一样,但有更多我自己的控制。

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

其他布局元素......