【问题标题】:How to display multiple fragments in one activity?如何在一个活动中显示多个片段?
【发布时间】:2019-07-08 07:16:08
【问题描述】:

我的问题与这个有关:

Multiple Fragments in One Activity

我在平板电脑上的一个活动中显示三个片段:两个只包含文本(成分和步骤),一个是 Exoplayer 和文本。所有片段都包含在各自的框架布局中。我使用约束布局作为父布局。由于成分片段中文本的长度,ExoPlayer 在纵向和横向模式下的显示并不统一。它应该始终位于该片段的右侧和 Steps 片段的上方。其他两个 Fragment 正确显示在屏幕左侧。

我曾考虑过使用线性布局并设置布局权重,但后来阅读了几篇不建议这样做的帖子。在这种布局中显示 ExoPlayer 片段的最佳方式是什么?先感谢您。

<?xml version="1.0" encoding="utf-8"?>

<ScrollView 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"
    android:id="@+id/scroll"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/tablet_detail_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        tools:context="annin.my.android.bakingapp.ui.IngredientStepsActivity">
        <!--
        This layout is a two-pane layout for the master/detail flow.
        -->
    <FrameLayout
            android:id="@+id/ingredients_fragment_container"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginStart="10dp"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintBottom_toTopOf="@+id/steps_fragment_container"
            app:layout_constraintVertical_weight="0.4"
            >

        </FrameLayout>

        <FrameLayout
            android:id="@+id/video_fragment_container"
            android:layout_width="wrap_content"
            android:layout_height="250dp"
            android:layout_marginStart="5dp"
            android:layout_marginTop="80dp"
            android:layout_marginEnd="60dp"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintStart_toEndOf="@+id/ingredients_fragment_container"
            app:layout_constraintVertical_weight="0.4" >

        </FrameLayout>

        <FrameLayout
            android:id="@+id/steps_fragment_container"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="10dp"
            android:layout_marginEnd="40dp"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/ingredients_fragment_container"
            app:layout_constraintVertical_weight="0.4"
           >

        </FrameLayout>
    </android.support.constraint.ConstraintLayout>
</ScrollView>

【问题讨论】:

  • 为每个框架布局添加适当的约束。我看到你错过了 video_fragment_container 等的结束约束。除非为每个孩子设置了所有四个(开始、结束、顶部、底部)约束,否则约束布局将无法正常工作。

标签: android android-fragments android-linearlayout android-constraintlayout android-layout-weight


【解决方案1】:

它非常简单,只需制作 2 个片段,然后将它们分配给各自的片段文件,如下所示:

<fragment
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/my_fragment"
    android:name="annin.my.android.bakingapp.ui.Fragment1" />

<fragment
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/my_fragment_2"
    android:name="annin.my.android.bakingapp.ui.Fragment2" />

【讨论】:

  • 但是片段被包围在框架布局中,而不是
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-10-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多