简体   繁体   English

如何更改操作栏的颜色(actionbarsherlock)

[英]How to change the color of actionbar (actionbarsherlock)

How to change the color of actionbar (actionbarsherlock) ? 如何更改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). 这个按钮的背景颜色也是白色(因为Sherlock主题)。 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. 在设置actionBar的样式时,最好的方法是使用非常受欢迎的Action Bar Style Generator ,它是一个Web应用程序,可让您预览ActionBar的外观,并让您将所有必需的资源下载到简单将它添加到您的产品:9patches,state-drawables和.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. 使用它是确保操作栏看起来很棒的最佳方法,而不会浪费大量时间用于有价值的Java代码。

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 . 我认为你应该使用@style/Widget.Sherlock.Light.ActionBar.Solid@style/Widget.Sherlock.Light.ActionBar.Solid.Inverse作为ActionBarStyle的父级,并且属性backgroundandroid: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. ActionBar样式生成器可以帮助您设置子菜单样式。

 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. 您首次发布的内容几乎完全正确,这是您需要添加以更改ActionBar颜色的主题。 You should also add the android:background attribute with your color: 您还应该使用您的颜色添加android:background属性:

<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. 但是,您需要确保将其应用于整个应用程序,或仅应用于要显示它的活动。您可以使用android在AndroidManifest.xml中的应用程序标记或活动标记中执行此操作:主题属性。

<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. 我假设您在具有HC或更高版本的设备上测试了您的应用程序,并且因为您没有为本机ActionBar指定背景,所以它不起作用。 Change it the the style above and it should work. 改变它上面的风格,它应该工作。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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