在 LinearLayout 视图中折叠和滚动视图

Collapsing and scrolling Views in a LinearLayout View

我正在尝试在软键盘打开时在包含两个 Fragment 的 Android 布局中创建一个非常具体的行为,如下图从左到右所示。

片段A是一个需要软键盘输入的表格,片段B是一个摄影幻灯片,只是作为视觉填充。

蓝色轮廓代表根视图。 Fragment A为固定高度,Fragment B填充剩余的垂直space。当键盘打开时,片段 B 折叠直到其高度为 0,此时片段 A 变为可滚动。

我试过以下方法:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
  <FrameLayout
      android:id="@+id/FragmentB"
      android:layout_width="match_parent"
      android:layout_height="0dp"
      android:layout_weight="1" />
  <ScrollView
      android:layout_width="match_parent"
      android:layout_height="wrap_content">
    <FrameLayout
        android:id="@+id/FragmentA"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
  </ScrollView>
</LinearLayout>

在此视图中,Fragment B 在软键盘打开时保持相同的高度,而 Fragment A 折叠。

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
  <LinearLayout
      android:orientation="vertical"
      android:layout_width="match_parent"
      android:layout_height="match_parent">
    <FrameLayout
        android:id="@+id/FragmentB"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1" />
    <FrameLayout
        android:id="@+id/FragmentA"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
  </LinearLayout>
</ScrollView>

并且在这个视图中,Fragment B 不断折叠,并且在 Fragment A 下面的视图底部留有间隙。

如何实现我在上图中布局的布局行为?

谢谢!

我在这里找到了解决方案:

Min height fill_parent and height wrap_content in ScrollView? 我的结果代码是:

<ScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true">  <!-- FillViewport ensures the scrollview doesn't wrap the content -->
  <LinearLayout
      android:orientation="vertical"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:layout_weight="1"> <!-- Layout weight 1 means the LinearLayout will at least fill the scrollview, or wrap content if too big -->
    <FrameLayout
        android:id="@+id/FragmentB"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1" />
    <FrameLayout
        android:id="@+id/FragmentA"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
  </LinearLayout>
</ScrollView>