首页 > 解决方案 > 使用后退按钮更改导航抽屉按钮后无法恢复

问题描述

当我将其更改为后退按钮后返回时,它会消失并显示以下代码:

ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(false);

DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);

ActionBarDrawerToggle mToggle = new ActionBarDrawerToggle(this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
mToggle.setDrawerIndicatorEnabled(true);

drawer.setDrawerListener(mToggle);
drawer.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);

我认为该代码应该使打开菜单的导航抽屉按钮再次出现,但事实并非如此。

知道该代码中可能有什么问题,所以导航抽屉按钮再次出现?

标签: androidnavigation-drawer

解决方案


试试这样:

Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

if (getSupportActionBar() != null) getSupportActionBar().setDisplayHomeAsUpEnabled(true);

//.....

此外,您可以使用androidx.navigation.ui.NavigationUI包来设置您的抽屉菜单。

是关于 NavigationUI 的好文章。


推荐阅读