Change ActionBarSherlock background color

痞子三分冷 提交于 2019-12-28 02:32:27

问题


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.

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

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.


回答1:


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. ColorDrawable did 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.




回答2:


I just used

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

It changed the background color. Hope it helps.




回答3:


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+

To solve that error:

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



来源:https://stackoverflow.com/questions/10064411/change-actionbarsherlock-background-color

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!