Android 更改操作栏标签背景颜色

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11318750/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 06:56:04  来源:igfitidea点击:

Change ActionBar Tabs background color

androidandroid-actionbarandroid-ui

提问by Maria Neumayer

Is there a way to change the background color of the tab bar in the ActionBar without changing it in the one line version?

有没有办法改变ActionBar中标签栏的背景颜色而不用在一行版本中改变它?

To clarify what I want: In portrait mode the ActionBar is split in two lines, the ActionBar itself and the tabs below. In landscape mode the tabs are in the actual ActionBar.

澄清我想要什么:在纵向模式下,ActionBar 分为两行,ActionBar 本身和下面的选项卡。在横向模式下,选项卡位于实际的 ActionBar 中。

I want to change the background color of the portrait mode. If I change the background in the TabView it'll be changed for both modes. Do I have to create separate styles for those? Which brings up a second question: is there a way to know when it'll be two lines and when not?

我想更改纵向模式的背景颜色。如果我更改 TabView 中的背景,这两种模式都会更改。我是否必须为这些创建单独的样式?这就引出了第二个问题:有没有办法知道什么时候是两行,什么时候不是?

Or am I just missing something?

或者我只是错过了什么?

I'm using ActionBarSherlock btw

我正在使用 ActionBarSherlock 顺便说一句

回答by JesperB

I think you are looking for the android:backgroundStackedattribute of the ActionBar style:

我认为您正在寻找android:backgroundStackedActionBar 样式的属性:

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

<style name="MyActionBarStyle" parent="@android:style/Widget.Holo.Light.ActionBar.Solid">
    <item name="android:backgroundStacked">@drawable/my_stacked_background</item>
</style>

or (If using ActionBarSherlock):

或(如果使用 ActionBarSherlock):

<style name="MyTheme" parent="@style/Theme.Sherlock.Light">
    <item name="android:actionBarStyle">@style/MyActionBarStyle</item>
    <item name="actionBarStyle">@style/MyActionBarStyle</item>
</style>

<style name="MyActionBarStyle" parent="@style/Widget.Sherlock.Light.ActionBar.Solid">
    <item name="android:backgroundStacked">@drawable/my_stacked_background</item>
    <item name="backgroundStacked">@drawable/my_stacked_background</item>
</style>

回答by Srikanth P

To Change Actionbar Tab's color, Pls use this code:

要更改操作栏选项卡的颜色,请使用以下代码:

//For Example if you want White color as Tabs background, then

//例如,如果你想要白色作为标签背景,那么

getActionBar().setStackedBackgroundDrawable(new ColorDrawable(Color.parseColor("#FFFFFF")));

回答by vasart

In ActionBarSherlock's values/abs__themes.xmlthere is

在 ActionBarSherlock 中values/abs__themes.xml

<item name="actionModeSplitBackground">@drawable/abs__cab_background_bottom_holo_dark</item>

You have to create your own theme derived from ABS

您必须创建自己的源自 ABS 的主题

<style name="AppTheme" parent="Theme.Sherlock">
    <item name="actionBarStyle">@style/MyActionBar</item>
    <item name="android:actionBarStyle">@style/MyActionBar</item>
</style>

<style name="MyActionBar" parent="Widget.Sherlock.ActionBar">
    <item name="actionModeSplitBackground">@drawable/my_split_background</item>
</style>

Hope this helps you.

希望这对你有帮助。

回答by megaKertz

ColorDrawable colorDrawable = new ColorDrawable(Color.White);
actionBar.SetStackedBackgroundDrawable(colorDrawable);

for xamarin folks.

对于 xamarin 的人。

回答by Mouna Cheikhna

To seperate styles depending on orientation you have to create in your /resfolder a new folder called layout-land(for landscape mode) and layout-port(in portrait mode) and put your xml files for action bar and set it's specific style (with the color you want) on each folder.

要根据方向分离样式,您必须在/res文件夹中创建一个名为layout-land(横向模式)和layout-port(纵向模式)的新文件夹,并将您的 xml 文件放在操作栏上,并在每个文件夹上设置它的特定样式(使用您想要的颜色)文件夹。