从导航列表动作条更改背景颜色代码(Change ActionBar Background Color

2019-06-26 17:08发布

我想,当用户选择在导航列表选择来更改操作栏背景的颜色。

目前,我的代码如下所示:

@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,它改变了动作条颜色为白色。


我第二次单击导航列表中itemPosition 1,我没有问题。


谁能告诉我这是为什么,我该如何解决这一问题? 感谢您的帮助!

Answer 1:

试试这个:

myActivity.invalidateOptionsMenu();


Answer 2:

请尝试使用此代码:

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


Answer 3:

我有同样的问题。

在Visual Studio或类似Xamarin用户。

请刚的setContentView(Resource.Layou后粘贴......在活动课。

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

如你认为有必要做更多的动作条变量你可以改变它。

由于希望这有助于。



文章来源: Change ActionBar Background Color in code from Navigation List