android的布局文件fragment标签。这个fragment在navigation标签内包裹,在哪能查看该标签允许的属性,和相关属性的解释
这个是我用的文档,翻了一会儿,也不知道在哪
https://developer.android.google.cn/docs?hl=zh-cn
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
app:startDestination="@id/fragment_first">
<fragment
android:id="@+id/fragment_first"
android:name="com.google.samples.apps.sunflower.test.FirstFragment"
tools:layout="@layout/fragment_first">
<action
android:id="@+id/action_first_to_second"
app:destination="@id/fragment_second"
app:enterAnim="@anim/fragment_fade_enter"
app:exitAnim="@anim/fragment_fade_exit"
app:launchSingleTop="true"
app:popEnterAnim="@anim/fragment_close_enter"
app:popExitAnim="@anim/fragment_close_exit"/>
<argument
android:name="name"
android:defaultValue="admin"
app:argType="com.google.samples.apps.sunflower.test.FirstViewModel"
app:nullable="false" />
<deepLink app:uri="test://first" />
</fragment>
....
</navigation>
属性解释:
fun actionFirstToSecond(): NavDirections = ActionOnlyNavDirections(R.id.action_first_to_second)
Navigation.findNavController(view).navigate(Uri.parse("test://first"))
<androidx.fragment.app.FragmentContainerView
android:id="@+id/nav_host"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:navGraph="@navigation/nav_test" />
属性解释:
跳转 Fragment:
//使用build 项目时生成的 XXXDirections对象指定跳转
Navigation.findNavController(view).navigate(ThirdFragmentDirections.actionThirdToFourth().actionId)
// 使用 deepLink 跳转
Navigation.findNavController(view).navigate(Uri.parse("test://first"))
返回 fragment:
// 指定跳转到的fragment,若第二个参数(inclusive)为true时,也会关闭指定的fragment,返回到指定的fragment前一个fragment;为false时,则返回到指定fragment,该方法会把在指定fragment之后的fragment都给pop出栈.
Navigation.findNavController(view).popBackStack(R.id.fragment_first, false)
//关闭当前的fragment
Navigation.findNavController(view).popBackStack()
经过查阅官方文档和资料,我认为目前并没有一个明确的地方可以查看android的布局文件fragment标签在navigation标签内包裹时所允许的属性。不过,在官方文档中有详细的解释与示例,我们可以通过官方文档来了解和学习如何使用navigation框架。在使用的时候,需要注意以下几点:
创建Navigation Resource File,然后在其中声明Fragment。在Fragment标签中,可以设置以下属性:
android:id:用于指定该Fragment的ID
tools:layout:用于指定该Fragment所使用的布局文件
在Navigation Graph标签中,使用Fragment标签,声明每个Fragment的信息。在Fragment标签中,可以设置以下属性:
android:id:用于指定该Fragment的ID
app:popExitAnim:用于指定返回时的退出动画
在使用Navigation框架时,可以使用以下方法:
Navigation.findNavController(view):用于获取当前视图所在的NavController
需要注意的是,使用Navigation框架时,需要在Activity中添加一个FragmentContainerView,并设置其属性,以指定该Activity所使用的Navigation Graph。同时,需要在代码中获取该视图所在的NavController并进行跳转或返回操作。