如何在导航控制器中更改目的地的标签属性? [英] How to change label attribute of a destination in navigation controller?

查看:50
本文介绍了如何在导航控制器中更改目的地的标签属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在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"
}

这篇关于如何在导航控制器中更改目的地的标签属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆