【问题标题】:Could you tell me why the author need to wrap FrameLayout for Fragment?你能告诉我为什么作者需要为 Fragment 包装 FrameLayout 吗?
【发布时间】: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


【解决方案1】:

你能告诉我为什么作者需要为 Fragment 包装 FrameLayout 吗?

不需要FrameLayoutFrameLayout 最初可能因为其他原因而存在(例如,通过 FragmentTransaction 更改内容)并且从未被删除。

【讨论】:

    猜你喜欢
    • 2015-09-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-14
    • 1970-01-01
    相关资源
    最近更新 更多