使用带有jetpack导航组件的导航抽屉时如何更改工具栏图标(汉堡包图标)

Bli*_*ide 5 android navigation-drawer hamburger-menu android-jetpack android-jetpack-navigation

我正在使用 jetpack 导航组件来构建导航抽屉,我想更改工具栏的汉堡包图标,我尝试了很多解决方案,例如波纹管,但它们不起作用

app:navigationIcon="@drawable/menu"
Run Code Online (Sandbox Code Playgroud)

getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeAsUpIndicator(R.drawable.menu);
Run Code Online (Sandbox Code Playgroud)

这是我的代码

 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);
}
Run Code Online (Sandbox Code Playgroud)

Gab*_*tti 6

您可以使用addOnDestinationChangedListener

    navController.addOnDestinationChangedListener { controller, destination, arguments ->
        if (destination.id == R.id.nav_xxxx){
            supportActionBar?.setHomeAsUpIndicator(R.drawable.xxxx)
        }
    }
Run Code Online (Sandbox Code Playgroud)