Android 如何更改操作栏标题的背景色
更改整个操作栏的背景色很容易,或者只更改操作栏标题的文本颜色,但我发现没有办法更改标题的背景色,如下所示: 我试过:Android 如何更改操作栏标题的背景色,android,android-actionbar,Android,Android Actionbar,更改整个操作栏的背景色很容易,或者只更改操作栏标题的文本颜色,但我发现没有办法更改标题的背景色,如下所示: 我试过: <style name="MyActionBar.TitleTextStyle" parent="@android:style/TextAppearance.Holo.Widget.ActionBar.Title"> <item name="android:textColor">#000</item> <item nam
<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>
#000
5px
#CC0000
但是只有
textColor
有效在onCreate()上尝试以下代码
首先,您需要为应用程序声明自定义主题(或活动,具体取决于您的需要)。类似于
<!-- Somewhere in AndroidManifest.xml -->
<application ... android:theme="@style/ThemeSelector">
然后,为两种情况声明您的自定义主题,有和没有Holo主题的API版本。对于旧主题,我们将自定义windowTitleBackgroundStyle属性,对于新主题,我们将自定义ActionBarStyle属性
@样式/窗标题背景
@颜色/标题/背景
@样式/操作栏
@颜色/标题/背景
就这样!这里我们使用@color/title\u background作为背景。它也可以是可绘制的,您还可以自定义其他属性。我创建了一个ImageView,其中操作栏将使用2个相对位置(外部为0填充,内部为普通填充)。然后,我将ImageView添加到外部布局,并从那里操纵它,使其像一个动作栏一样工作,但更多的是由我自己控制 android:layout\u width=“match\u parent” android:layout\u height=“match\u parent” android:background=“#ffffffff” 工具:context=“dayOfWeekApp.MainActivity” > 其他布局元素
actionbar.setBackgroundDrawable(新彩色Drawable(0xffFFFF00));不,这会更改所有动作baractionbar.setTitle(Html.fromHtml(“+getString(R.string.app_name)+”)的背景;这将更改标题的颜色,而不是背景颜色。设置图像…设置徽标这将更改所有操作栏的背景颜色。我只想更改标题的背景色。请尝试以下代码来查找title int titleId=getResources().getIdentifier(“action\u bar\u title”、“id”、“android”);TextView txtTitle=(TextView)findViewById(titleId);然后设置背景
<!-- Somewhere in AndroidManifest.xml -->
<application ... android:theme="@style/ThemeSelector">
<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>
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"
>