如何使视图填满剩余空间

5
所以我的屏幕有两个“部分”。我有顶部是ScrollView,底部是随机文本的底部。底部的随机文本将始终更改大小,因此有时它可能需要40dp,有时需要20dp等。
我的问题是,是否有办法使底部部分动态化(不会丢失文本),并使顶部ScrollView适应新的大小,并根据底部部分使用的部分限制其自身的大小以“填充剩余空间”?
像这样:
如您所见,滚动视图仅填充可用的剩余空间。
我正在寻找仅使用XML的解决方案。
需要帮助!
2个回答

12

你可以尝试这个。

<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"/>

或者

<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"/>


5

将两个视图都放在一个垂直的LinearLayout 中;

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

而位于底部的视图: height = "wrap_content"


非常感谢!解释得很好! - 4444borja

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接