ScrollView 无法正确调整自身大小

ScrollView not sizing itself correctly

我正在尝试让 ScrollView 占用尽可能多的屏幕 space,直到它开始将其下方(外部)的项目推出屏幕,然后它需要停止扩展并成为滚动。

<?xml version="1.0" encoding="utf-8"?>
<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="wrap_content"
          android:background="#ff0000"
    >
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="taking\nup\lots\nof\nlines\nto\nmake\nthe\nscrollview&quot;s\ncontent\ntaller\nthan\nthe\nspace\navailable\nto\nit\nwhich\nshould\nmake\nthe\nscrollview\nstop\nabove\nthe\nbutton\nand\nbecome\nscrollable"
        /> 
        <!-- 
            android:text="just one line"
        -->
    </ScrollView>

    <Button
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:text="Button"
    />
</LinearLayout>

因为它位于 ScrollView 上方,所以会填满整个屏幕高度并将按钮推离屏幕底部。 如果我将 android:layout_weight="1" 添加到 ScrollView 那么它适用于这种情况 - 按钮位于底部并且 ScrollView 停止在它上面 - 但是当 ScrollView 没有太多内容时(用单行替换文本) 那么 ScrollView 不会缩小以适应内容,所以太高了。

我尝试使用 RelativeLayout 但没有成功 - 如果 Button 是 android:layout_below ScrollView,那么如果它有很多内容,ScrollView 会将它推离屏幕底部。

这是我想要的样子:在第一张图片中,ScrollView 有很多内容,因此会扩展以填充可用高度,但不会将其下方的项目(按钮)推离屏幕,在第二张图片 ScrollView 没有太多内容,所以只占用它需要的高度,允许它下面的项目(按钮)向上移动屏幕:

试试这个:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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="match_parent"
        android:layout_above="@+id/button"
        android:background="#ff0000">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">
            <TextView
                android:padding="5dp"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="taking\nup\lots\nof\nlines\nto\nmake\nthe\nscrollview&quot;s\ncontent\ntaller\nthan\nthe\nspace\navailable\nto\nit\nwhich\nshould\nmake\nthe\nscrollview\nstop\nabove\nthe\nbutton\nand\nbecome\nscrollable" />
            <TextView
                android:padding="5dp"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="taking\nup\lots\nof\nlines\nto\nmake\nthe\nscrollview&quot;s\ncontent\ntaller\nthan\nthe\nspace\navailable\nto\nit\nwhich\nshould\nmake\nthe\nscrollview\nstop\nabove\nthe\nbutton\nand\nbecome\nscrollable" />
            <TextView
                android:padding="5dp"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="taking\nup\lots\nof\nlines\nto\nmake\nthe\nscrollview&quot;s\ncontent\ntaller\nthan\nthe\nspace\navailable\nto\nit\nwhich\nshould\nmake\nthe\nscrollview\nstop\nabove\nthe\nbutton\nand\nbecome\nscrollable" />
            <TextView
                android:padding="5dp"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="taking\nup\lots\nof\nlines\nto\nmake\nthe\nscrollview&quot;s\ncontent\ntaller\nthan\nthe\nspace\navailable\nto\nit\nwhich\nshould\nmake\nthe\nscrollview\nstop\nabove\nthe\nbutton\nand\nbecome\nscrollable" />
            <TextView
                android:padding="5dp"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="taking\nup\lots\nof\nlines\nto\nmake\nthe\nscrollview&quot;s\ncontent\ntaller\nthan\nthe\nspace\navailable\nto\nit\nwhich\nshould\nmake\nthe\nscrollview\nstop\nabove\nthe\nbutton\nand\nbecome\nscrollable" />
            <TextView
                android:padding="5dp"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="taking\nup\lots\nof\nlines\nto\nmake\nthe\nscrollview&quot;s\ncontent\ntaller\nthan\nthe\nspace\navailable\nto\nit\nwhich\nshould\nmake\nthe\nscrollview\nstop\nabove\nthe\nbutton\nand\nbecome\nscrollable" />
            <TextView
                android:padding="5dp"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="taking\nup\lots\nof\nlines\nto\nmake\nthe\nscrollview&quot;s\ncontent\ntaller\nthan\nthe\nspace\navailable\nto\nit\nwhich\nshould\nmake\nthe\nscrollview\nstop\nabove\nthe\nbutton\nand\nbecome\nscrollable" />
            <TextView
                android:padding="5dp"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="taking\nup\lots\nof\nlines\nto\nmake\nthe\nscrollview&quot;s\ncontent\ntaller\nthan\nthe\nspace\navailable\nto\nit\nwhich\nshould\nmake\nthe\nscrollview\nstop\nabove\nthe\nbutton\nand\nbecome\nscrollable" />
        </LinearLayout>
    </ScrollView>
    <Button
        android:id="@+id/button"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:text="Button" />
</RelativeLayout>

您可以做的是更正代码中的高度。这有点老套,我想看看另一种解决方案,但在我的脑海中,我不知道有什么更好的了。

您需要添加一个 OnGlobalLayoutListener 并在其中计算 ScrollView 高度或 ScrollView 周围容器的高度减去你的 Button.

然后在 ScrollView 上使用 setLayoutParams() 设置大小。

并且您必须删除侦听器以避免无限循环:-)

    final View scrollview = findViewById(R.id.scrollview);
    final View container = findViewById(R.id.container);
    final View button = findViewById(R.id.button);
    scrollview.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            int height = scrollview.getHeight();
            int heightButton = button.getHeight();
            int heightContainer = container.getHeight();
            int min = Math.min(heightContainer - heightButton, height);
            int width = scrollview.getWidth();
            Log.v("test", "min: " + min);
            scrollview.setLayoutParams(new RelativeLayout.LayoutParams(width, min));

            // do not forget to remove the listener
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                scrollview.getViewTreeObserver().removeOnGlobalLayoutListener(this);
            }
            else {
                scrollview.getViewTreeObserver().removeGlobalOnLayoutListener(this);
            }
        }
    });

为此,您必须在布局文件中使用 wrap_content 作为 ScrollView 的高度。并且外部容器必须是 RelativeLayout 以便呈现 Button 并且具有非零高度!

如果您使用填充或边距,则必须在计算中考虑这些值。

只需尝试在滚动视图上添加 fillViewport = "true"