在code从导航列表动作条更改背景颜色 [英] Change ActionBar Background Color in code from Navigation List
本文介绍了在code从导航列表动作条更改背景颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我要当用户选择在导航列表中进行选择,以改变操作栏背景的颜色。
I want to change the color of the Action Bar background when the user chooses a selection in the Navigation List.
目前,我的code是这样的:
Currently, my code looks like this:
@Override
public boolean onNavigationItemSelected(int itemPosition, long itemId) {
ColorDrawable colorDrawable = new ColorDrawable();
ActionBar actionBar = getActionBar();
if(itemPosition == 0)
{
colorDrawable.setColor(0xffFEBB31);
actionBar.setBackgroundDrawable(colorDrawable);
return true;
}
if(itemPosition == 1)
{
colorDrawable.setColor(0xff9ACC00);
actionBar.setBackgroundDrawable(colorDrawable);
return true;
}
return false;
}
不过,我第一次在导航列表中选择itemPosition 1,它改变了动作条颜色为白色。
However, the first time I select itemPosition 1 in the Navigation List, it changes the ActionBar color to white.
我第二次单击导航列表中的itemPosition 1,我没有任何问题。
The second time I click the itemPosition 1 in the Navigation List, I have no issue.
谁能告诉我这是为什么,我怎么能解决这个问题?感谢您的帮助!
Could anyone tell me why this is and how I can fix the problem? Thank you for the help!
推荐答案
试试这个:
myActivity.invalidateOptionsMenu();
这篇关于在code从导航列表动作条更改背景颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文