使用 AndroidX 导航的 Fragment 中的动态 ActionBar 标题 [英] Dynamic ActionBar title from a Fragment using AndroidX Navigation

查看:47
本文介绍了使用 AndroidX 导航的 Fragment 中的动态 ActionBar 标题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用来自 Android Jetpack 的新 Navigation 组件.>

根活动设置非常简单:

override fun onCreate(savedInstanceState: Bundle?) {super.onCreate(savedInstanceState)setContentView(R.layout.activity_main)setSupportActionBar(工具栏)val navController = findNavController(R.id.navigationFragment)setupActionBarWithNavController(navController)bottomNavigationView.setupWithNavController(navController)}

当 Fragment 的标题在导航图中定义时,它运行良好.但是对于一个 Fragment,我想动态设置标题.

我尝试了 findNavController().currentDestination.label = "Hello world" 但它什么也没做.

我当然可以使用像 (activity as? AppCompatActivity)?.supportActionBar?.title = "Hello world" 这样的技巧,但我觉得它会打破 setupActionBarWithNavController 的魔力() 对我有用.有没有办法动态更新操作栏标题?

解决方案

1.0.0-alpha08 开始,您可以让 NavigationUI 位动态设置标题...如果动态位是关于导航操作的参数.

例如,在您的导航图中,您可以有这样的内容:

 <片段android:id="@+id/displayFragment"android:name="com.commonsware.jetpack.sampler.nav.DisplayFragment"android:label="标题: {title}" ><论证android:name="modelId"app:argType="string"/><论证机器人:名称=标题"app:argType="string"/></片段>

这里,android:label 属性有一个用大括号括起来的参数名称({title} in "Title: {title}".然后应用栏的标题将设置为标签的值,{title} 替换为 title 的值 参数.

如果您需要比这更复杂的东西—例如,您想通过 ID 查找模型并从中读取属性 —您将需要使用更多手动方法,例如此问题的其他答案中概述的方法.

I am using the new Navigation component from Android Jetpack.

The root Activity setup is quite simple:

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)
    setSupportActionBar(toolbar)

    val navController = findNavController(R.id.navigationFragment)
    setupActionBarWithNavController(navController)

    bottomNavigationView.setupWithNavController(navController)
}

It works well when the Fragment's title is defined in the navigation graph. But for one Fragment, I want to set the title dynamically.

I tried with findNavController().currentDestination.label = "Hello world" but it does nothing.

I could of course use a trick like (activity as? AppCompatActivity)?.supportActionBar?.title = "Hello world", but I feel that it will break the magic that setupActionBarWithNavController() does for me. It there any way to update the Action Bar title dynamically?

解决方案

As of 1.0.0-alpha08, you can have the NavigationUI bits dynamically set the title... if the dynamic bits are arguments on the navigation action.

So, for example, in your navigation graph, you could have something like this:

  <fragment
    android:id="@+id/displayFragment"
    android:name="com.commonsware.jetpack.sampler.nav.DisplayFragment"
    android:label="Title: {title}" >
    <argument
      android:name="modelId"
      app:argType="string" />
    <argument
      android:name="title"
      app:argType="string" />
  </fragment>

Here, the android:label attribute for our <fragment> has an argument name wrapped in braces ({title} in "Title: {title}". The app bar's title will then be set to the value of the label, with {title} replaced by the value of the title argument.

If you need something more elaborate than that — for example, you want to look up the model by ID and read a property from it — you will need to use more manual approaches, such as those outlined in other answers to this question.

这篇关于使用 AndroidX 导航的 Fragment 中的动态 ActionBar 标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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