我使用了我在这里找到的解决方案让Action Bar上的左按钮作为Drawer Toggle,当有背板和后退按钮时没有任何东西。这是实现
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity);
ButterKnife.bind(this);
setSupportActionBar(mToolbar);
final ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.setDisplayShowHomeEnabled(true);
actionBar.setDisplayHomeAsUpEnabled(true);
}
navigateTo(R.id.menu_drawer);
initDrawer();
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, mToolbar,
R.string.main_open_menu, R.string.main_close_menu)
{
public void onDrawerClosed(View view){
syncActionBarState();
}
public void onDrawerOpened(View view){
mDrawerToggle.setDrawerIndicatorEnabled(true);
}
};
mOnBackStackChangedListener =
new android.support.v4.app.FragmentManager.OnBackStackChangedListener() {
@Override
public void onBackStackChanged() {
syncActionBarState();
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
getSupportFragmentManager().addOnBackStackChangedListener(mOnBackStackChangedListener);
}
private void syncActionBarState(){
int backStackCount = getSupportFragmentManager().getBackStackEntryCount();
mDrawerToggle.setDrawerIndicatorEnabled(backStackCount == 0);
if(backStackCount == 0){
mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);
}else{
mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
}
}
作为抽屉切换按钮工作正常。当作为后退按钮工作时,它甚至不会触发OnOptionsItemClickListener。我错过了什么?
答案 0 :(得分:1)
您已将setDisplayHomeAsUpEnabled()
方法实施为
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
因此您可以将其设为described here,这是一种简单的方法