Android toolbar setNavigationIcon not working

自作多情 提交于 2019-12-19 02:05:36

问题


So, I have a BaseActivity in which I have a toolbar and I call setSupportActionBar(toolbar).

In some of my activities that extends BaseActivity, I would like to change the navigation icon (the default arrow) to another drawable. But when I call toolbar.setNavigationIcon(myDrawable) it doesn't work, it still shows the default left pointing arrow icon.

Any idea? Thanks.


回答1:


I think you can set like this

    menuDrawerToggle = new ActionBarDrawerToggle(this, menuDrawer, toolbar, R.string.drawer_open, R.string.drawer_close){...}

    menuDrawerToggle.syncState();

    toolbar.setNavigationIcon(getResources().getDrawable(yourDrawable));

put setNavigationIcon after syncState()




回答2:


In my case: I don`t use ActionBarDrawerToggle. For me helpful was: to change order of methods calls.

From:

Toolbar toolbar = (Toolbar)getActivity().findViewById(R.id.toolbar);
((AppCompatActivity)getActivity()).setSupportActionBar(toolbar);
toolbar.setNavigationIcon(R.drawable.ic_chevron_left_white_24dp);

To:

Toolbar toolbar = (Toolbar)getActivity().findViewById(R.id.toolbar);
toolbar.setNavigationIcon(R.drawable.ic_chevron_left_white_24dp);
((AppCompatActivity)getActivity()).setSupportActionBar(toolbar);



回答3:


In my case, setNavigationIcon after syncState as @Hsieh not work! My solution is set in onPostCreate method as below. Override this method in your activity

  @Override
    protected void onPostCreate(@Nullable Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        mToolbar.setNavigationIcon(R.drawable.ic_menu_button);
    }


来源:https://stackoverflow.com/questions/26641259/android-toolbar-setnavigationicon-not-working

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