简体   繁体   中英

How to change the color of actionbar (actionbarsherlock)

How to change the color of actionbar (actionbarsherlock) ?

I've been tried below code but no luck.

<style name="MyTheme.ActionBarStyle" parent="Widget.Sherlock.ActionBar">
    <item name="background">#ffffffff</item>
</style>

<style name="Theme.SherlockCustom" parent="@style/Theme.Sherlock.Light">
    <item name="actionBarStyle">@style/MyTheme.ActionBarStyle</item>
</style>

+and one thing more, i'm add submenu like below. this button's background color is also white(because Sherlock Theme). could i change those colors?

    SubMenu sub = menu.add("abcd");
    sub.add(0, 1, 0, "Default");
    sub.add(0, 2, 0, "Light");
    sub.add(0, 3, 0, "Light (Dark Action Bar)");
    sub.getItem().setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS | MenuItem.SHOW_AS_ACTION_WITH_TEXT);

When it comes to styling the actionBar, the best shot you have is to use the very popular Action Bar Style Generator which is a web app that will let you preview the way the ActionBar will look, and let you download all the necessary assets to simply add it to your product: 9patches, state-drawables, and .xmls.

If you are not sure how to use that, here's a quick guide .

Using this is the best shot at making sure the action bar looks great, without wasting much of your time that could be best spent on valuable Java Code.

I think you should use @style/Widget.Sherlock.Light.ActionBar.Solid or @style/Widget.Sherlock.Light.ActionBar.Solid.Inverse as parent for ActionBarStyle and both attributes background and android:background . For example:

<style name="MyTheme.ActionBarStyle" parent="@style/Widget.Sherlock.Light.ActionBar.Solid">
    <item name="background">#ffffff</item>
    <item name="android:background">#ffffff</item>
</style>

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

ActionBar style generator may help you with styling sub menus.

 I just used below Code 

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

 it changed the bg color. Hope, it helps.

What you first posted looks almost exactly right, that is the theme that you need to add to change the ActionBar color. You should also add the android:background attribute with your color:

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

<style name="Theme.SherlockCustom" parent="@style/Theme.Sherlock.Light">
    <item name="actionBarStyle">@style/MyTheme.ActionBarStyle</item>
</style>

However, you need to make sure you apply it either to your whole application, or just to the activity that you want to display it in. You can do this in either the application tag or the activity tag in AndroidManifest.xml with the android:theme attribute.

<application
    android:name="com.your.package.name"
    android:icon="@drawable/icon"
    android:label="@string/app_name"
    android:theme="@style/Theme.SherlockCustom" >

Use this style:

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

I assume you tested your application on a device with HC or above, and since you didn't specify a background for the native ActionBar, it did not work. Change it the the style above and it should work.

创建自定义动作栏,然后你将动作栏的颜色变化Sherlock .....

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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