【发布时间】:2021-03-30 22:39:47
【问题描述】:
我有一个布局层次结构,其中包括 ScrollView 顶部的一些内容和该内容下方的嵌套片段(使用 FragmentContainerView)。
我想要的是片段填充空间或超出空间,具体取决于内容。我原本希望将子片段的布局分别设置为match_parent 和wrap_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