【问题标题】:LinearLayout cropping but not expanding in ScrollviewLinearLayout 裁剪但在 Scrollview 中不展开
【发布时间】:2026-01-19 06:00:01
【问题描述】:

大家好,

我一直在到处寻找,但似乎找不到解决方案。我在 LinearLayout 内的 ScrollView 内使用 LinearLayout,LinearLayout -> ScrollView -> LinearLayout。但是它不会在我想要的时候展开,而是裁剪我的布局。

有人知道我在这里缺少什么吗?可能有点乱,但尝试了很多东西

<?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:background="#FFFFFF"
android:orientation="vertical">

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <include
            android:id="@+id/image_slider_menu"
            layout="@layout/image_slider"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="3" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:background="@color/menu"
            android:orientation="horizontal"
            android:gravity="center_vertical">

            <ImageView
                android:id="@+id/bars_map"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="4"
                android:padding="10dp"
                android:src="@drawable/maps" />

            <TextView
                android:id="@+id/report"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="8"
                android:gravity="left|center_vertical"
                android:text="Meld Misbruik"
                android:clickable="true" />

        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="3"
            android:orientation="vertical">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                android:padding="15dp">

                <TextView
                    android:id="@+id/bar_view_title"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="title"
                    android:textSize="22sp"
                    android:textStyle="bold" />

                <TextView
                    android:id="@+id/bar_view_description"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="description"
                    android:textSize="14sp" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@color/menu"
                android:orientation="vertical"
                android:paddingLeft="15dp"
                android:paddingTop="5dp">

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="Contact"
                    android:textSize="22sp"
                    android:textStyle="bold" />

                <TextView
                    android:id="@+id/bar_view_contact"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="Gegevens" />
            </LinearLayout>
        </LinearLayout>


    </LinearLayout>
</ScrollView>
</LinearLayout>

【问题讨论】:

  • 按照你们的建议进行了更改。它已展开(文本在底部消失),但我无法在那里滚动。我现在要回家了,我稍后再看看。无论如何,谢谢!
  • 问题在于权重值。我删除了它们,给出了一些高度并包装了内容,现在它又开始滚动了....但是这并没有使它响应。

标签: android xml scrollview android-linearlayout android-layout-weight


【解决方案1】:

如果内容太长,您的 LinearLayout 无法调整大小,因为您设置了固定大小。

只是改变:

android:layout_height="0dp"
android:layout_weight="1"

作者:

 android:layout_height="wrap_content"

【讨论】:

  • 这样做了,它被扩展了。但它不会滚动。
  • 你的 layout/image_slider 是什么?
  • 这是一个自定义图像滑块。如果我考虑一下,可能会导致问题。我会用它尝试一些东西。
  • 不,这不是问题。我将其更改为 NetworkImageView 和常规 ImageView。
  • 尝试删除它只是为了测试,但我很确定这是图像滑块和滚动视图之间的冲突
【解决方案2】:

没有遗漏任何东西,这是正确的行为方式,不幸的是......您的 LinearLayout 应该有没有重量的 wrap_content,请检查:ScrollView's Handy Trick

您也可以同时测量 SCrollView 和 Linear,如果 svHeight>llHeight 则 ll.getLayoutParams().height=sv.height

编辑: 好的,给你一些方便的代码:

<ScrollView
    android:id="@+id/sv"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

<LinearLayout
    android:id="@+id/ll"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">
...

在代码中:

sv.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {

        @SuppressLint("NewApi")
        @Override
        public void onGlobalLayout() {
            if(Build.VERSION.SDK_INT<16)
                sv.getViewTreeObserver().removeGlobalOnLayoutListener(this);
//misspell :D
            else
                sv.getViewTreeObserver().removeOnGlobalLayoutListener(this);
            int svHeight=sv.getMesuredHeight();
            if(svHeight>ll.getMeasuredHeight())
                ll.getLayoutParams().height=svHeight;
        }
}

【讨论】:

  • 哪个线性?第一还是第二?
  • 第二,这个在ScrollView里面。首先是不必要的,lint 可能会警告你(如果你真的使用静态背景颜色)
  • ~ 没关系。 sv.getMeasuredHeight() 中存在拼写错误。
  • 仍然不滚动:(。我会在谷歌上搜索更多内容并在我得到它的工作时更新我的​​答案。无论如何谢谢:)
【解决方案3】:

问题在于权重值。我删除了它们,给出了一些高度并包装了内容,现在它再次滚动......但这并没有使其响应。

这确实回答了我的问题,但不需要我想要的。但是布局看起来足够好,即使在不同的分辨率下,这就是为什么我把它留成这样。

【讨论】: