【问题标题】:How to make View fill remaining space如何使视图填充剩余空间
【发布时间】:2018-06-25 17:49:31
【问题描述】:

所以我的屏幕上有两个“部分”。我有一个滚动视图的顶部和一个随机文本的底部。底部的随机文本总是会改变大小,所以有时需要 40dp,有时需要 20dp,等等。

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

像这样:

如您所见,滚动视图只是填满了剩余的可用空间。

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

需要帮助!

【问题讨论】:

    标签: android xml


    【解决方案1】:

    你可以试试这个。

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

    【讨论】:

      【解决方案2】:

      将两个视图包裹在一个垂直的LinearLayout

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

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

      【讨论】:

      • 非常感谢!很好的解释
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-27
      • 2021-10-29
      • 1970-01-01
      相关资源
      最近更新 更多