How to change label attribute of a destination in navigation controller?

陌路散爱 提交于 2019-12-24 04:28:12

问题


I am trying to use navigation controller in Android

as you can see in the image above, I set the label attribute of my destination to be 'Home'.

and this label, will show like title in my toolbar. can I change that label programatically ? because I want to set my toolbar title dynamically.

I have tried to change the toolbar title using toolbar.title = "some title here" but it will always be overlapped the title from that label.

so how to solve this ?


回答1:


Do it in your activity like below it's worked for me:

 setSupportActionBar(toolbar)
    val navController = findNavController(R.id.nav_controller_fragment)
    val appBarConfiguration = AppBarConfiguration(navController.graph)
    setupActionBarWithNavController(navController, appBarConfiguration)


    navController.addOnDestinationChangedListener { controller, destination, arguments ->
        when (destination.id) {
            R.id.mainFragment -> toolbar.title = "ok"
            else -> {
                toolbar.title = "General"
            }
        }
    }

or if you want to change from your fragment do like below:

 override fun onStart() {
    super.onStart()
    (activity as MainActivity).toolbar.title = "changed"
}


来源:https://stackoverflow.com/questions/54646720/how-to-change-label-attribute-of-a-destination-in-navigation-controller

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!