android - ScrollView 无法正确调整自身大小

标签 android android-layout android-scrollview

我试图让 ScrollView 占据所需的屏幕空间,直到它开始将其下方(外部)的项目推离屏幕,然后它需要停止扩展并变得滚动。

<?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,但没有成功 - 如果按钮是android:layout_below ScrollView,那么当ScrollView有很多内容时,ScrollView会将其推离屏幕底部。

我希望它看起来像这样:在第一张图片中,ScrollView 有很多内容,因此会扩展以填充可用高度,但不会将其下方的项目(按钮)推离屏幕,在第二张图片中ScrollView 没有太多内容,因此仅占据所需的高度,允许其下方的项目(按钮)向上移动屏幕: ScrollView has a lot of content SrollView doesn't have much content

最佳答案

您可以做的是纠正代码中的高度。这有点老套,我希望看到另一个解决方案,但我不知道有什么更好的解决方案。

您需要添加一个 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 并且具有非零高度!

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

关于android - ScrollView 无法正确调整自身大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30194450/

相关文章:

java - Android spinner Data Binding 使用 XML 并显示选定的值

android - 我们可以在 Android MotionLayout 的同一个 <Transition> 中使用 OnSwipe 和 OnClick 吗?

android - 检测点击操作栏后退按钮 -(点击操作栏后退按钮时 OnOptionsItemSelected 不调用)

android - 为什么 android 不允许我在我自己的线程中更新我的 UI

android - 你如何创建一个带有多个后置摄像头的安卓模拟器

android - 如何检查 .realm 数据库是否已经存在?

android - 当 ListView 行项目中有隐藏 View 时, fragment 不尊重匹配父高度

android - SwipeRefreshLayout 干扰 ViewPager 中的 ScrollView

android - 将触摸事件从 ScrollView 委托(delegate)给垂直 ViewPager

android - ScrollView 中的 View 中的 OnTouchListener