使用AndroidX导航的片段中的动态ActionBar标题 [英] Dynamic ActionBar title from a Fragment using AndroidX Navigation
问题描述
我正在使用Android Jetpack中新的导航组件.
>根活动的设置非常简单:
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)
}
在导航图中定义片段的标题时,它可以很好地工作.但是对于一个片段,我想动态设置标题.
我尝试了findNavController().currentDestination.label = "Hello world"
,但是它什么也没做.
我当然可以使用(activity as? AppCompatActivity)?.supportActionBar?.title = "Hello world"
之类的技巧,但是我觉得它将打破setupActionBarWithNavController()
对我的神奇作用.是否有任何方法可以动态更新操作栏标题?
从1.0.0-alpha08
开始,如果动态位是导航操作的参数,则可以让NavigationUI位动态设置标题.... >
例如,在导航图中,您可能会遇到类似这样的情况:
<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>
在这里,我们<fragment>
的android:label
属性具有用大括号括起来的参数名称("Title: {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导航的片段中的动态ActionBar标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!