如何在 android 中关闭导航抽屉

How to close the Navigation drawer in android

我的应用程序有一个导航抽屉。从抽屉选项中,我正在打开不同的活动。那时在新 Activity.

之前显示空白屏幕

@Override public boolean onNavigationItemSelected(@NonNull MenuItem item) {

switch (item.getItemId()) {
    case R.id.nav_message:

        Intent newAct = new Intent(getApplicationContext(), FeedbackActivity.class);
        startActivity(newAct);
    
       break;


}


drawer.closeDrawer(Gravity.RIGHT);
return true;

}

根据一些调查,我发现我们需要从中删除以下代码。试过一样,工作正常。

drawer.closeDrawer(Gravity.RIGHT);
    return true;

但是当我点击新的后退按钮时activity,抽屉仍然处于打开状态。没有黑屏怎么关闭?

您是否尝试过将 GRAVITY.START 作为参数传递给 drawer.close(int gravity),如下所示:

switch (item.getItemId()) {
    case R.id.nav_message:

        Intent newAct = new Intent(getApplicationContext(), FeedbackActivity.class);
        startActivity(newAct);
    
       break;


}



drawer.closeDrawer(GravityCompat.START);
return true;

并且不要忘记用 findViewById()

初始化抽屉对象

尝试禁用抽屉菜单动画并在开始新菜单之前调用 closeDrawer activity

    drawer.closeDrawer(Gravity.RIGHT, false);
    switch (item.getItemId()) {
    case R.id.nav_message:

        Intent newAct = new Intent(getApplicationContext(), FeedbackActivity.class);
        startActivity(newAct);
    
       break;


}

return true;