【发布时间】:2020-01-13 08:33:21
【问题描述】:
代码 A 来自 activity_main.xml。它是sample project 的一部分。
不知道作者为什么要为Fragment包裹FrameLayout, 在我去掉代码A的FrameLayout后得到代码B,代码B运行良好。
你能告诉我为什么作者需要为 Fragment 包装 FrameLayout 吗?
代码 A
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:keepScreenOn="true"
tools:context=".MainActivity">
<fragment
android:id="@+id/fragment_container"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:navGraph="@navigation/nav_graph"
app:defaultNavHost="true"/>
</FrameLayout>
代码 B
<?xml version="1.0" encoding="utf-8"?>
<fragment
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/fragment_container"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:navGraph="@navigation/nav_graph"
app:defaultNavHost="true"/>
【问题讨论】:
-
也许只是为了
android:keepScreenOn? -
@HelloCW 代码 B 对于 NavHost 来说已经足够了。
标签: android android-studio android-fragments