FrameLayout是否应作为不同片段的容器

问题描述

我对Android中的Fragments设计有个一般性的疑问。我的应用程序中有一个BottomNavigationBar,我打算使用Jetpack导航组件(尚未完成)。我想要一个活动和多个片段(因为这是每个人都告诉我使用的内容)。现在,不同的片段应该显示在容器中,对吗?我可以为此使用FrameLayout并将所有片段放入其中吗?当然,片段不应同时显示,而应取决于选择的片段。我为“活动”创建了一个示例XML布局文件,该文件应作为容器,并在其中插入了两个“片段”。

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/bottom_navigation"
        app:labelVisibilityMode="labeled"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/colorGreen"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:menu="@menu/bottom_navigation"
        app:itemIconTint="@color/colorPrimaryDark"
        app:itemTextColor="@color/colorAccent"
        />




    <FrameLayout
        android:id="@+id/container_frame"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:layout_constraintBottom_toTopOf="@+id/bottom_navigation"
        app:layout_constraintTop_toTopOf="parent"

    <fragment
        android:id="@+id/info_fragment"
        android:name="com.example.td.barapp.Info_Fragment"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:layout_constraintBottom_toTopOf="@+id/bottom_navigation"
        app:layout_constraintTop_toTopOf="parent"

        ></fragment>

    <fragment
        android:id="@+id/info_fragment2"
        android:name="com.example.td.barapp.Info_Fragment2"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:layout_constraintBottom_toTopOf="@+id/bottom_navigation"
        app:layout_constraintTop_toTopOf="parent"

        ></fragment>
        />

</androidx.constraintlayout.widget.ConstraintLayout>

这是正确的方法吗?或者,单个活动还可以如何服务多个片段?我必须以某种方式将它们链接到活动,对吧?

我会很感激每条评论,非常感谢您的帮助。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)