How to change programmatically background color of action bar items

可紊 提交于 2019-12-19 07:09:26

问题


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?


回答1:


Have you tried the solution from user3225831 mentioned here: https://stackoverflow.com/a/21297231/1738838

ActionBar bar = getActionBar();
bar.setBackgroundDrawable(new ColorDrawable("COLOR")); 



回答2:


Use simple one line code... Pass RGB values of the required color

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




回答3:


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.



来源:https://stackoverflow.com/questions/25081706/how-to-change-programmatically-background-color-of-action-bar-items

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