【问题标题】:change actionbar color and icons based on condition根据条件更改操作栏颜色和图标
【发布时间】:2017-12-15 02:10:16
【问题描述】:

我创建了一个带有导航抽屉的应用程序。我想更改操作栏的颜色和抽屉的默认图标,以便在选择特定导航菜单项时更改,它应该在选择任何其他项目时更改为默认值。

我的代码

Fragment fragment;
if(fragment.toString().contains("HomeFragment")){
            ab = ((BaseActivity)getApplicationContext()).getSupportActionBar();
            ab.setHomeButtonEnabled(true);
            ab.setDisplayHomeAsUpEnabled(true);
            ab.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#FFFFFF")));
            ab.setElevation(0);
            ab.setDisplayHomeAsUpEnabled(true);
            ab.setHomeAsUpIndicator(R.drawable.username);
    }

我可以使用上面的代码更改操作栏颜色和导航抽屉图标,但不能更改为其他导航项选择的默认颜色和图标。

【问题讨论】:

  • 片段永远不会被初始化。无论如何在你的片段 findviewbyid 和 setonclicklistener onclick myviewitem.setbackground...
  • 那不是完整的代码。片段已初始化并且工作正常。当我选择“HomeFragment”时,操作栏的颜色正在改变。但我无法更改导航抽屉图标。

标签: android navigation-drawer


【解决方案1】:

继续你的代码

Fragment fragment;
if(fragment.toString().contains("HomeFragment")){
            ab = ((BaseActivity)getApplicationContext()).getSupportActionBar();
            ab.setHomeButtonEnabled(true);
            ab.setDisplayHomeAsUpEnabled(true);
            ab.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#FFFFFF")));
            ab.setElevation(0);
            ab.setDisplayHomeAsUpEnabled(true);
            ab.setHomeAsUpIndicator(R.drawable.username);
    } else {
      ab = ((BaseActivity)getApplicationContext()).getSupportActionBar();
            ab.setHomeButtonEnabled(true);
            ab.setDisplayHomeAsUpEnabled(true);
            ab.setBackgroundDrawable(new ColorDrawable(Color.parseColor("your default color")));
            ab.setElevation(0);
            //ab.setHomeAsUpIndicator(R.drawable.username); change it to your other icon;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-04-08
    • 2013-04-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多