【发布时间】:2020-07-03 20:24:29
【问题描述】:
所以我使用最新的 NavigationView 作为导航抽屉以及自定义工具栏图标。当我启动我的应用程序时,我可以在工具栏(操作栏)中看到我的自定义图标,但是一旦我通过单击导航抽屉中的菜单项切换到不同的片段,我的工具栏就会重置回汉堡图标并且它永远不会设置我的自定义图标无论我在 NavigationDrawer 项目之间切换多少次,都会返回。下面是我的代码-
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
DrawerLayout drawer = findViewById(R.id.drawer_layout);
NavigationView navigationView = findViewById(R.id.nav_view);
View hView = navigationView.getHeaderView(0);
// Passing each menu ID as a set of Ids because each menu should be considered as top level destinations.
mAppBarConfiguration = new AppBarConfiguration.Builder(
R.id.nav_home, R.id.nav_score, R.id.nav_history,
R.id.nav_setting)
.setDrawerLayout(drawer)
.build();
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
NavigationUI.setupWithNavController(navigationView, navController);
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeAsUpIndicator(R.drawable.ic_custom_icon);//your icon here
如果需要,请提供其他信息...
- 我正在使用新的材质依赖 'com.google.android.material:material:1.1.0'
- 我的基本应用程序主题是 style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar"
我更改了上面代码的顺序,看看有什么错误(有人建议在old stackoverflow posts中)。
【问题讨论】:
-
如果您不希望图标自动更改,为什么还要使用
setupActionBarWithNavController?
标签: android android-actionbar toolbar android-toolbar android-architecture-navigation