简体   繁体   English

如何更改操作栏标题的背景颜色

[英]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 但是只有textColor有效

尝试在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. 然后,针对两种情况(带有和不带有Holo主题的API版本)声明自定义主题。 For the old themes we'll customize the windowTitleBackgroundStyle attribute, and for the newer ones the ActionBarStyle. 对于旧主题,我们将自定义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>

That's it! 而已! Here we use @color/title_background as a background. 在这里,我们使用@ color / title_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). 我通过使用2个RelativeLayouts(外部为0填充,内部为普通填充)创建了一个ActionBar所在的ImageView。 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. 然后,我将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"
    >

Other layout elements... 其他布局元素...

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

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