如何使View填充剩余空间

ray*_*bay 3 xml android

所以我的屏幕上有两个"部分".我有顶部部分是ScrollView,底部是随机文本.底部的随机文本将始终改变大小,因此有时可能需要40dp,有时20dp等.

我的问题是,是否有办法使底部动态(不丢失文本)并使顶部scrollview适应新的大小并限制其自己的大小以"填充剩余空间"基于底部的哪个部分是使用?

像这样:

在此输入图像描述 在此输入图像描述

如您所见,滚动视图仅填充剩余的可用空间.

我正在寻找仅使用XML的解决方案

需要帮忙!

小智 11

你可以试试这个.

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

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_above="@+id/bottomTextView">

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

        <TextView
            android:layout_width="match_parent"
            android:layout_height="500dp"
            android:background="@android:color/holo_red_dark"
            android:text="Test" />
    </LinearLayout>
</ScrollView>

<TextView
    android:id="@+id/bottomTextView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:background="@android:color/darker_gray"/>
Run Code Online (Sandbox Code Playgroud)

要么

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1">

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

        <TextView
            android:layout_width="match_parent"
            android:layout_height="500dp"
            android:background="@android:color/holo_red_dark"
            android:text="Test" />
    </LinearLayout>
</ScrollView>

<TextView
    android:id="@+id/bottomTextView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@android:color/darker_gray"/>
Run Code Online (Sandbox Code Playgroud)


Eld*_*tov 5

将两个视图垂直包裹LinearLayout

顶部的视图:height = "0dp",weight = 1

以及底部的视图:height = "wrap_content"