Android 单击工具栏上的汉堡图标不会打开导航抽屉

Android 单击工具栏上的汉堡图标不会打开导航抽屉,android,navigation-drawer,android-optionsmenu,hamburger-menu,Android,Navigation Drawer,Android Optionsmenu,Hamburger Menu,我有一个导航抽屉,它工作得非常好。 重构我的代码我删除了活动中的所有选项ItemSelecteds,并使所有活动从基本活动继承,该基本活动扩展了AppCompletActivity,并实现了所有必要的方法。 在此之后,即使我有syncstate()和所有东西,单击汉堡包图标也不再有效 有什么线索说明这不起作用吗? 其中一项活动: }您正在为ActionBarDrawerToggle使用四参数构造函数,这意味着您必须在MainActivity的OnOptions ItemSelected()覆盖中

我有一个导航抽屉,它工作得非常好。 重构我的代码我删除了活动中的所有选项ItemSelecteds,并使所有活动从基本活动继承,该基本活动扩展了AppCompletActivity,并实现了所有必要的方法。 在此之后,即使我有
syncstate
()和所有东西,单击汉堡包图标也不再有效

有什么线索说明这不起作用吗?

其中一项活动:


}

您正在为
ActionBarDrawerToggle
使用四参数构造函数,这意味着您必须在
MainActivity
OnOptions ItemSelected()
覆盖中调用切换的
OnOptions ItemSelected()
方法才能打开/关闭抽屉

例如:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    if(mDrawerToggle.onOptionsItemSelected(item)) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}
Toolbar toolbar = findViewById(R.id.toolbar);
ActionBarDrawerToggle mDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout,
        toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);

如果您碰巧提供了自己的
工具栏
——例如,作为支持
操作栏
(尽管没有必要将其设置为这样),那么您可以将该
工具栏
作为
ActionBarDrawerToggle
构造函数调用中的第三个参数传递。例如:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    if(mDrawerToggle.onOptionsItemSelected(item)) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}
Toolbar toolbar = findViewById(R.id.toolbar);
ActionBarDrawerToggle mDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout,
        toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
然后,抽屉的打开/关闭将由
ActionBarDrawerToggle
内部处理,您无需调用
OnOptions ItemSelected()
中的切换

此设置也不需要调用
setDisplayHomeAsUpEnabled()
,如果您不想将
工具栏
设置为
操作栏
,此调用非常方便