Android 更改 ActionBarSherlock 背景颜色

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10064411/
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 02:21:34  来源:igfitidea点击:

Change ActionBarSherlock background color

androidandroid-uiactionbarsherlock

提问by Kris B

I'm trying to implement ActionBarSherlock because I was told it is relatively easy to implement and customize. I've found it was pretty easy to implement, but I'm trying to change the background color of the ActionBar and it's proving difficult.

我正在尝试实施 ActionBarSherlock,因为有人告诉我它相对容易实施和定制。我发现它很容易实现,但我正在尝试更改 ActionBar 的背景颜色,但事实证明这很困难。

According to the the site (link), it seems you can inherit one of the ActionBarSherlock's themes and then override the properties you need.

根据该站点(链接),您似乎可以继承 ActionBarSherlock 的主题之一,然后覆盖您需要的属性。

This is what I have so far:

这是我到目前为止:

<?xml version="1.0" encoding="UTF-8"?>
<resources>
    <style name="Theme.ActionBar" parent="Theme.Sherlock.ForceOverflow">
      <item name="android:background">#000000</item>
      <item name="background">#000000</item>
    </style>
</resources>

I'm noticing the built-in theme are using images for the background, but I'm praying I don't have to create images to change the background color.

我注意到内置主题使用图像作为背景,但我祈祷我不必创建图像来更改背景颜色。

Thanks.

谢谢。

回答by Jake Wharton

The action bar background color is defined in a style for the action bar, not in the theme itself. You'll need to do something like this:

操作栏背景颜色是在操作栏的样式中定义的,而不是在主题本身中定义的。你需要做这样的事情:

<style name="Theme.MyTheme" parent="Theme.Sherlock.ForceOverflow">
    <item name="actionBarStyle">@style/Widget.MyTheme.ActionBar</item>
    <item name="android:actionBarStyle">@style/Widget.MyTheme.ActionBar</item>
</style>

<style name="Widget.MyTheme.ActionBar" parent="Widget.Sherlock.ActionBar">
    <item name="android:background">#ff000000</item>
    <item name="background">#ff000000</item>
</style>

Be careful using colors defined in XML. ColorDrawabledid not respect it's view bounds on pre-Honeycomb so if you use tab navigation with a separate background for the stacked tab view you will have problems.

使用 XML 中定义的颜色时要小心。ColorDrawable不尊重它在蜂窝之前的视图边界,所以如果你使用带有单独背景的标签导航来堆叠标签视图,你会遇到问题。

回答by alicanbatur

I just used

我刚用

getSupportActionBar().setBackgroundDrawable(new ColorDrawable(Color.parseColor("#00853c")));

It changed the background color. Hope it helps.

它改变了背景颜色。希望能帮助到你。

回答by aagam94

The code mentioned by Jake Wharton is true. But when applying the code to the styles.xml may not work if you have minSDK<11 because android:actionBarStyle is supported in API-11+

Jake Wharton 提到的代码是真实的。但是,如果您有 minSDK<11,则将代码应用到 style.xml 时可能不起作用,因为 API-11+ 支持 android:actionBarStyle

To solve that error:

要解决该错误:

Make a folder of values-v11 in your res folder and create the XML file as mentioned above.

在 res 文件夹中创建一个 values-v11 文件夹并创建上述 XML 文件。