如何在导航控制器中更改目的地的标签属性?
我正在尝试在Android中使用导航控制器
I am trying to use navigation controller in Android
如上图所示,我将目的地的label
属性设置为家".
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.
我尝试使用toolbar.title = "some title here"
更改工具栏标题,但它始终与该标签上的标题重叠.
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.
那么如何解决呢?
在您的活动中进行操作,如下所示:
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"
}