Android 如何以编程方式更改操作栏项目的背景颜色
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25081706/
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
How to change programmatically background color of action bar items
提问by Tomasz
It is easy to set a default color of the items background in action bar by setting:
通过设置,很容易在操作栏中设置项目背景的默认颜色:
<item name="android:actionBarItemBackground">@drawable/action_bar_item_background</item>
in application theme.
在应用主题中。
I would like to change this color only for one of my fragments. How to do it programmatically?
我只想为我的一个片段更改此颜色。如何以编程方式做到这一点?
回答by Elementary
Have you tried the solution from user3225831 mentioned here: https://stackoverflow.com/a/21297231/1738838
您是否尝试过这里提到的 user3225831 的解决方案:https://stackoverflow.com/a/21297231/1738838
ActionBar bar = getActionBar();
bar.setBackgroundDrawable(new ColorDrawable("COLOR"));
回答by Srikanth P
Use simple one line code... Pass RGB values of the required color
使用简单的一行代码...传递所需颜色的 RGB 值
getActionBar().setBackgroundDrawable(new ColorDrawable(Color.rgb(248, 248, 248)));
For Color codes, you can use this website: http://www.rapidtables.com/web/color/RGB_Color.htm
对于颜色代码,您可以使用此网站:http: //www.rapidtables.com/web/color/RGB_Color.htm
回答by Tomasz
I have found a simple workaround of this problem. You can set transparent color to the background of action bar item. Then changing color of the action bar will change background of the item. I don't see possibility to change item color without recreating an the activity.
我找到了解决此问题的简单方法。您可以为操作栏项目的背景设置透明颜色。然后更改操作栏的颜色将更改项目的背景。我看不到在不重新创建活动的情况下更改项目颜色的可能性。