android - FrameLayout 是否应该是不同片段的容器
问题描述
我有一个关于 Android 中 Fragments 设计的一般性问题。我的应用程序中有一个 BottomNavigationBar,我打算使用 Jetpack 导航组件(我还没有这样做)。我想要一个活动和多个片段(因为这是每个人都告诉我使用的)。现在不同的片段应该显示在一个容器中,对吧?我可以为此使用 FrameLayout 并将所有片段放入其中吗?当然,片段不应该同时显示,而是取决于选择了哪个片段。我已经为应该用作容器的 Activity 创建了一个示例 XML 布局文件,并在其中插入了两个 Fragment。
<?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>
这是正确的方法吗?或者一个 Activity 还能如何服务多个 Fragment?我必须以某种方式将它们与活动联系起来,对吗?
我会很感激每一条评论,并非常感谢您的帮助。
解决方案
推荐阅读
- ios - 如何将 Json 数据发送到表视图数组?迅速
- reactjs - 重新启动页面后重置“上下文”中的变量
- sqlite - 带有 RETURNING 语句的 INSERT 上的 sqlite_reset() 回滚/取消它
- algorithm - 基于优先级值解决时间线冲突的算法
- c++ - 如何使用二叉搜索树指针纠正 C++ 内存泄漏?
- mysql - 如何通过查看上月名,将缺失的月份名依次添加到sql表中?
- android - Whatsapp 链接
- ios - AVAudioSession 将类别设置为没有选项的播放无法在 AirPods 上播放,也设置没有选项的类别将不会应用媒体信息
- python - 我无法从 flask_sqlalchemy 和 flask_wtf.csrf 导入,即使我为两者都安装了 pip。在我得到一台新电脑之前它正在工作
- java - 在 Netbeans IDE 中,我得到一个 Javadoc not found 错误,我已经阅读了类似的问题,但没有一个给出的解决方案有效