【问题标题】:Android Scrollview Won't fill parent spaceAndroid Scrollview不会填充父空间
【发布时间】:2016-01-16 07:29:06
【问题描述】:

我有一个相对布局填满整个屏幕。我想在它的中间添加一个滚动视图来包装一堆内容,以便在显示软键盘时让它平移。但是,一旦我将它包装在滚动视图中,最底部的布局就会停止填充屏幕的其余部分。

这是我将 ScrollView 注释掉的 XML。

<include
    android:id="@+id/top_bar_with_save_button"
    layout="@layout/top_bar_with_save_button"/>

<FrameLayout
    android:id="@+id/log_entry_title_frame"
    android:layout_below="@id/top_bar_with_save_button"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@id/log_entry_title_frame"
    android:layout_alignParentBottom="true">

<!--
    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">
-->

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_below="@+id/log_entry_title_frame"
            android:orientation="vertical"
            android:background="#f00">

            <!-- Lots of stuff in here -->
            <EditText
                android:id="@+id/log_entry_notes"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:padding="8dp"
                android:gravity="top|left"/>

        </RelativeLayout>

<!--
    </ScrollView>
 -->
</LinearLayout>

看起来像这样:

但是,一旦我从 ScrollView 中删除评论,它就会立即像这样压缩:

为什么会这样?我需要它来填充屏幕上的整个空间,但我无法弄清楚发生了什么。谁能告诉我如何解决这个问题?谢谢!

【问题讨论】:

    标签: android android-layout android-scrollview


    【解决方案1】:

    您需要将fillViewport="true" 属性添加到您的ScrollView 标记中。或者您可以使用scrollView.setFillViewPort(true); 以编程方式添加它。否则,ScrollView 将换行到其子项的内容高度。

    【讨论】:

      【解决方案2】:
      <ScrollView
          android:layout_width="match_parent"
          android:layout_height="match_parent">
          <RelativeLayout
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  ... >
      

      滚动容器的layout_height应该是“wrap_content”。

      【讨论】:

      • wrap_content 填满整个空间? wrap_content 正好相反 - 它使用尽可能少的空间来显示其内容。
      • wrap_content 用于获取滚动内容的总高度。
      猜你喜欢
      • 2021-06-17
      • 1970-01-01
      • 1970-01-01
      • 2012-07-15
      • 1970-01-01
      • 2021-01-04
      • 1970-01-01
      相关资源
      最近更新 更多