Up arrow does not show after calling ActionBarDrawerToggle.setDrawerIndicatorEnabled(false)

匿名 (未验证) 提交于 2019-12-03 01:31:01

问题:

The title says it all. When I call the mDrawerToggle.setDrawerIndicatorEnabled(false) I don't want the "hamburger" icon to be shown anymore but the backwards navigation arrow.

Unfortunately when I call this method just the title is shown without the backwards arrow nor the "hamburger" icon. After setting the drawerIndicatorEnabled to be true again it shows the "hamburger" icon again.

I set getSupportActionBar().setDisplayHomeAsUpEnabled(true) and getSupportActionBar().setDisplayShowHomeEnabled(true)

Edit: Basically the solution suggested here: Change drawer icon back to back arrow somehow doesn't give me the back arrow.

Does anyone know a solution for this issue? Thank you very much!

回答1:

After hours of trials and errors I came up with a solution that allows to switch from "hamburger" to "arrow" and back. This is very weird and unnatural, don't ask me why it works in this way, but it works. Furthermore, this is the only solution that allowed me to do this, nothing else worked.

I have only one activity with fragments. When I'm switching from one fragment to another, I'm setting boolean variable in my activity displayingInnerFragment. For those fragments, where displayingInnerFragment == true, I show "arrow" in the top left corner, and for all others I show "hamburger". The following code I execute before switching to any fragment:

    ActionBar actionBar = getSupportActionBar();     if (displayingInnerFragment) {         actionBar.setDisplayHomeAsUpEnabled(false);         drawerToggle.setDrawerIndicatorEnabled(false);         actionBar.setDisplayHomeAsUpEnabled(true);     } else {         drawerToggle.setDrawerIndicatorEnabled(true);     } 

Note the double call to actionBar.setDisplayHomeAsUpEnabled() in one branch. This is required for drawerToggle.setDrawerIndicatorEnabled(false) to work. Otherwise it will not work properly. All other options either don't show "arrow" or hide "arrow" or "hamburger" at one moment or another.



回答2:

Use like this:

mDrawerToggle.setHomeAsUpIndicator(R.drawable.abc_ic_ab_back_mtrl_am_alpha); mDrawerToggle.setDrawerIndicatorEnabled(false); mDrawerToggle.syncState(); 


回答3:

This one stumped me for a while, as I knew there was a default up icon being used (from looking at the source of ActionBarDrawerToggle), and I wanted it to adopt the color set for R.attr.colorControlNormal. Amazingly, the order of these two lines is crucial for actually showing the up arrow:

mDrawerToggle.setDrawerIndicatorEnabled(false); getSupportActionBar().setDisplayHomeAsUpEnabled(true); 

setDrawerIndicatorEnabled must be called on your ActionBarDrawerToggle before setDisplayHomeAsUpEnabled is called on your ActionBar. Both lines must be called for the default (tinted) arrow to show.



回答4:

I also face the same problem (it's either the

Additional Info : I'm using Support library 25, so it might play a factor in here as I didn't test with previous library version. I'm using Toolbar set as action bar, and set up the ActionDrawerToggle with method that has Toolbar parameter in it. I only tested this on Android M device, though.

The code below is how I enabled/disable the navigation drawer (function is resides in NavigationDrawer Fragment).

public void setDrawerEnabled(final boolean enabled) {      int lockMode = enabled ? DrawerLayout.LOCK_MODE_UNLOCKED :             DrawerLayout.LOCK_MODE_LOCKED_CLOSED;      mDrawerLayout.setDrawerLockMode(lockMode);     mDrawerToggle.setDrawerIndicatorEnabled(enabled);      ActionBar actionBar = null;     if (getActivity() instanceof AppCompatActivity) {         actionBar = ((AppCompatActivity) getActivity()).getSupportActionBar();     }      if (actionBar != null) {         actionBar.setDisplayHomeAsUpEnabled(!enabled);         actionBar.setDefaultDisplayHomeAsUpEnabled(!enabled);         actionBar.setDisplayShowHomeEnabled(enabled);         actionBar.setHomeButtonEnabled(enabled);     }      mDrawerToggle.syncState();      mDrawerToggle.setToolbarNavigationClickListener(new View.OnClickListener() {         @Override         public void onClick(View view) {              if (!mDrawerToggle.isDrawerIndicatorEnabled())                 getActivity().onBackPressed();          }     }); } 

In my case, setting the actionBar.setHomeButtonEnabled(enabled); forces it to draw the menu icon (or else, in my case, when first fragment is resumed, there won't be any navigation icon), although it will stop rendering the mDrawerToggle.syncState(); after changing the navigation icons remedy that for me, though. Now both icons are shown correctly!

I hope this helps someone!



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