我正在使用jetpack导航组件来构建导航抽屉,我想更改工具栏的汉堡图标,我尝试了许多类似于bellow的解决方案,但它们不起作用
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
:
navController.addOnDestinationChangedListener { controller, destination, arguments ->
if (destination.id == R.id.nav_xxxx){
supportActionBar?.setHomeAsUpIndicator(R.drawable.xxxx)
}
}