【问题标题】:Centre a fragment or wrap its content in a ScrollView depending on its content根据其内容将片段居中或将其内容包装在 ScrollView 中
【发布时间】:2021-03-30 22:39:47
【问题描述】:

我有一个布局层次结构,其中包括 ScrollView 顶部的一些内容和该内容下方的嵌套片段(使用 FragmentContainerView)。

我想要的是片段填充空间或超出空间,具体取决于内容。我原本希望将子片段的布局分别设置为match_parentwrap_content 会起作用,但内容根本不会滚动。

目前我的布局如下:

父片段

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <View
            android:id="@+id/top_content"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <androidx.fragment.app.FragmentContainerView
            android:id="@+id/main_container"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_marginTop="@dimen/small_content_margin"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@id/top_content" />
    </androidx.constraintlayout.widget.ConstraintLayout>
</ScrollView>

子片段 1

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center" />
</FrameLayout>

子片段 2

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    ...
</FrameLayout>

此设置要么显示 子片段 1 中的内容居中,要么显示 子片段 2 中的内容从顶部包裹其内容,这正是我想要的。然而,这会阻止布局滚动,显然是因为FragmentContainerView 的高度有限。

是否有一个干净的解决方案,同时保留滚动或其他可以实现相同的解决方案(基于 XML,不是以编程方式)?

【问题讨论】:

    标签: android android-layout android-fragments


    【解决方案1】:
    <ScrollView
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:fillViewport="true">
    
      <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    
        <View
          android:id="@+id/top_content"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content" />
    
        <androidx.fragment.app.FragmentContainerView
          android:id="@+id/main_container"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:layout_marginTop="@dimen/small_content_margin"/>
      </androidx.constraintlayout.widget.ConstraintLayout>
    </ScrollView>
    

    子片段 2

    <LinearLayout
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:orientation="vertical">
    
      ...
      </LinearLayout>
    

    【讨论】:

    • 感谢您的建议,我以为这是答案,但不幸的是它仍然拒绝滚动!
    • 试试我的编辑,如果不是你想要的,请描述更多细节
    • 不幸的是不是,我想要的是有不同的子片段,如果它们的高度是match_parent,那么它们会填充容器,但如果它们的高度是wrap_content,它们会包装它们的内容。但是,在我的问题中尝试这样会导致 wrap_content 孩子无法滚动,它只是固定到位。
    • top_content 里面有什么。我将它的高度设置为 100dp 然后它工作了
    • 只有几个按钮,容器是wrap_content。奇怪,固定高度也行,我试试!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-12
    • 2012-12-17
    相关资源
    最近更新 更多