使用带有jetpack导航组件的导航抽屉时如何更改工具栏图标(汉堡图标) [英] how to change toolbar icon (hamburger icon) when using navigation drawer with jetpack navigation component
本文介绍了使用带有jetpack导航组件的导航抽屉时如何更改工具栏图标(汉堡图标)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用jetpack导航组件构建导航抽屉,我想更改工具栏的汉堡包图标,我尝试了许多解决方案,例如波纹管,但它们不起作用
I'm using jetpack navigation component to build a navigation drawer, I want to change hamburger icon of the toolbar, I tried many solutions like bellow but they don't work
app:navigationIcon="@drawable/menu"
也
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeAsUpIndicator(R.drawable.menu);
这是我的代码
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
setSupportActionBar(toolbar)
navController=findNavController(R.id.nav_host_fragment)
appBarConfiguration=AppBarConfiguration(setOf(R.id.nav_acceuil,R.id.nav_notifications,R.id.nav_gerer,R.id.nav_deconnexion),drawer_layout)
setupActionBarWithNavController(navController,appBarConfiguration)
nav_view.setupWithNavController(navController);
}
推荐答案
您可以使用 addOnDestinationChangedListener
:
You can use the addOnDestinationChangedListener
:
navController.addOnDestinationChangedListener { controller, destination, arguments ->
if (destination.id == R.id.nav_xxxx){
supportActionBar?.setHomeAsUpIndicator(R.drawable.xxxx)
}
}
这篇关于使用带有jetpack导航组件的导航抽屉时如何更改工具栏图标(汉堡图标)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文