【问题标题】:Fill vertical space between two views (+scroll)填充两个视图之间的垂直空间(+滚动)
【发布时间】:2015-05-07 05:28:22
【问题描述】:

我有 3 个视图。
1. 视图 A 具有固定高度并与屏幕顶部对齐。
2. View B 具有固定高度,并与屏幕底部对齐。
3. 视图 C 必须填充视图 A 和 B 之间的垂直空间,但必须是最小 X 高度。 如果 A 和 B 之间的垂直间距小于 Xdp,则必须出现垂直滚动条(并且视图 B 必须滚动,而不是粘在底部)。

到目前为止我所拥有的(有点简化),它可能是完全错误的:

<ScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:fillViewport="true">

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

        <RelativeLayout
            android:id="@+id/layout_banner_top"
            android:layout_width="match_parent"
            android:layout_alignParentTop="true"
            android:layout_height="@dimen/statistics_probanner_height">

         </RelativeLayout>

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content" <!-- UPD here as McAdam331 suggested -->
            android:minHeight="@dimen/statistics_circular_progress_container_height"
            android:layout_below="@+id/layout_banner_top"
            android:layout_above="@+id/layout_banner_bottom">

            <!-- here is some content -->

         </RelativeLayout>

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="@dimen/statistics_bottom_bar_height"
            android:id="@+id/layout_banner_bottom"
            android:layout_alignParentBottom="true">

        </RelativeLayout>
    </RelativeLayout>
</ScrollView>

外观如何:
Picture 1
Picture 2

当前,中间视图(视图 C)的 minHeight 设置为 600dp,但正如您在图 2 中看到的那样,视图 C 正在填充顶部和底部视图之间的可用空间,高度太小并且不会出现滚动。

可以实现吗?我怎样才能实现这种行为?

【问题讨论】:

  • 视图 C 的高度应为match_parent,以填充视图 A 和 B 之间的剩余空间。
  • @DerGolem 你是对的,这是在发布问题之前进行的一些实验。解决了这个问题,但它当然不起作用。视图 A 和 B 之间的空间正在填充,但 minHeight 和 scroll 似乎不起作用。
  • 600dp 似乎有点过分了。特别是在水平模式下。
  • 仅用于测试目的,以确保滚动条按预期显示并且视图的高度为 minHeight。

标签: android android-layout views


【解决方案1】:

问题是您将 height 和 minheight 设置为相同的值:

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="@dimen/statistics_circular_progress_container_height"
    android:minHeight="@dimen/statistics_circular_progress_container_height"
    android:layout_below="@+id/layout_banner_top"
    android:layout_above="@+id/layout_banner_bottom">

换句话说,视图不会超过您已经给它的高度。我要做的是将 layout_height 设置为 wrap_content,并保持 min_height 属性不变。

这样,如果空间大于给定的尺寸,它只会绑定在横幅的上方和下方。否则,它将保持该最小值。

【讨论】:

  • 试过了,但都一样 - 视图 C 只填充视图 A 和 B 之间的空间,但 minHeight 和滚动似乎不起作用(如图 2 所示)。
  • 横幅的高度是多少?也许横幅占据了太多空间,以至于您的内部布局没有空间可以填充?
  • 顶视图为 120dp,底视图为 78dp。如果没有空间可以填充,则中间的视图必须为 minHeight 并且必须出现垂直滚动条。
【解决方案2】:

已解决。

<?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"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:fillViewport="true">

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

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="@dimen/statistics_probanner_height"
            android:gravity="top">

        </RelativeLayout>

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:minHeight="@dimen/statistics_circular_progress_container_height">

        </RelativeLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="@dimen/statistics_bottom_bar_height"
            android:layout_gravity="bottom">
        </LinearLayout>

    </LinearLayout>
</ScrollView>

【讨论】: