Change ActionBar Background Color in code from Navigation List

﹥>﹥吖頭↗ 提交于 2019-12-18 03:44:06

问题


I want to change the color of the Action Bar background when the user chooses a selection in the Navigation List.

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;
    }

However, the first time I select itemPosition 1 in the Navigation List, it changes the ActionBar color to white.


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!

回答1:


Try this:

myActivity.invalidateOptionsMenu();



回答2:


Try using this code:

ColorDrawable colorDrawable = new ColorDrawable(Color.parseColor("#ffFEBB31"));
actionBar.setBackgroundDrawable(colorDrawable); 



回答3:


I was having this same problem.

For Xamarin Users in Visual Studio or the like.

Please paste this just after the SetContentView(Resource.Layou...... in the activity class.

// Setting ActionBar (Toolbar) background color natively var actionBar = this.ActionBar; actionBar.SetBackgroundDrawable(new ColorDrawable(Color.Black));

You may change it as you deem necessary of do more on the actionBar variable.

Thanks hope this helps.



来源:https://stackoverflow.com/questions/11770512/change-actionbar-background-color-in-code-from-navigation-list

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