ChatGPT解决这个技术问题 Extra ChatGPT

LinearLayout 未在 ScrollView 内扩展

我在具有 android:layout_height="fill_parent"ScrollView 中有一个 LinearLayout,但它没有扩展到 ScrollView 的完整高度。我的布局看起来像:

level    layout    layout_width    layout_height
1    LinearLayout    fill_parent    fill_parent
2    LinearLayout    fill_parent    wrap_content
3    (some irrelevant stuff)
2    ScrollView      fill_parent    fill_parent <-- this expands full height
3    LinearLayout    fill_parent    fill_parent <-- this does not (has orientation=vertical)
(following stuff probably are irrelevant, but just to be sure:)
4    TextView        fill_parent    fill_parent
4    LinearLayout    fill_parent    wrap_content

我可以看到 LinearLayout 没有扩展 ScrollView 的整个高度,因为在 Android Layout Editor 中的 Eclipse 中,如果我选择 ScrollView(在大纲面板中),它会以填充的红色边框突出显示屏幕到底部,但是当我选择 LinearLayout 时,它的突出显示不会扩展到屏幕底部。我怎样才能让它这样做?

我想要达到的效果是在其下方有一些文本和一个按钮(在第 4 级的 LinearLayout 内只有一个按钮)。文本可以大到需要滚动条,在这种情况下,我希望用户必须向下滚动才能看到按钮。如果文本不足以容纳滚动条,我希望包含按钮的 LinearLayout 贴在屏幕底部。

起初我认为我不应该发布完整的 XML,因为在一个问题中看到大量代码通常会让人拒绝。但是,这似乎是必要的,所以这里是完整的布局。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    xmlns:android="http://schemas.android.com/apk/res/android">
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:id="@+id/video_layout"
        android:focusable="true"
        style="@style/VideoLayout">
        <FrameLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:foreground="@android:drawable/ic_media_play"
            android:foregroundGravity="center">
            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/video_thumb"
                android:padding="5dip"
                android:background="#454545"/>
        </FrameLayout>
        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:focusable="true"
            style="@style/VideoTitle"
            android:id="@+id/video_title"
            android:layout_gravity="center"
            android:layout_weight="1"/>
    </LinearLayout>
    <ScrollView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1">
        <!-- this ScrollView expands the full height of the screen.
             However, the next LinearLayout does not expand the full height of the ScrollView -->
        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:layout_gravity="fill"
            android:orientation="vertical"
            android:id="@+id/info_layout">
            <TextView
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:id="@+id/info_body"
                style="@style/InfoText"/>
            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                style="@android:style/ButtonBar">
                    <Button
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center"
                        android:layout_weight="1"
                        android:text="@string/button_readmore"
                        android:id="@+id/btn_read_more"/>
            </LinearLayout>
        </LinearLayout>
    </ScrollView>
</LinearLayout>

目前我在有问题的 LinearLayout 上使用了 android:layout_gravity="bottom",这使得按钮无论如何都粘在屏幕底部。但这也使文本粘在屏幕底部,这并不是我想要的。

更新android:layout_gravity="bottom" 使ScrollView 无法滚动。其他想法?


F
Felix

最后自己找到了解决方案。问题不在于 LinearLayout,而在于 ScrollView(看起来很奇怪,考虑到 ScrollView 正在扩展,而 LinearLayout 没有)。

解决方案是在 ScrollView 上使用 android:fillViewport="true"


请注意,LinearLayout 将垂直扩展,但这不一定会反映在您的布局中,除非它包含使用 android:weight 占用额外空间的控件。
这真的很有帮助。感谢那。但如果没有此代码,有时效果很好。你知道为什么吗?
不适用于
病得很厉害,我坐了几个小时,搜索我的代码有什么问题。这个参数是为了什么?你什么时候想把它设置为假?
@Prasad 使您的线性布局高度与父级匹配
H
Hardik4560

我知道这篇文章很老了,对于那些不想使用 android:fillViewport="true" 的人,因为它有时不会在键盘上方显示编辑文本。使用相对布局而不是 LinearLayout 它解决了目的。


s
snctln

你能提供你的布局xml吗?这样做可以让人们以最小的努力重新创建问题。

您可能需要设置

android:layout_weight="1"

对于您要扩展的项目


谢谢您的回答。我试过你的建议,但没有奏效。我还更新了我的问题以包含布局 xml。
A
Aniruddh Parihar

解决方案是使用

android:fillViewport="true"

在滚动视图上,此外尝试使用

"wrap_content" 而不是 "fill_parent" 作为 "fill_parent"

现在已弃用。


B
Braj

我已经动态地得到了这个。我有一个 LinearLayout 并在其中使用 ScrollView 作为孩子。然后我再次使用LinearLayout并将您想要的任何视图添加到此LinearLayout,然后将此LinearLayout添加到ScrollView,最后将此ScrollView添加到LinearLayout。然后你可以在你的 ScrollView 中滚动,并且什么都不会留下可见的。

LinearLayout(Parent)--ScrollView(LinerLayout 的子级)--LinearLayout(ScrollView 的子级)--在这里添加 textView、Buttons、spinner 等任何你想要的。然后将此 LinearLyout 添加到 ScrollView。 Bcoz 只有一个适用于 ScrollView 的 CHILD,最后将此 ScrollView 添加到 LinearLyout。如果定义的区域超出屏幕尺寸,那么您将在 ScrollView 中获得一个 Scroll。


但是这种解决方案使 xml 更加复杂,并且需要更多时间来膨胀。
V
Vishal Pandey

就我而言,我没有给出

LinearLayout 的方向(ScrollView 的子级)

所以默认情况下它是水平的,但是scrollview垂直滚动,所以请检查'android:orientation =“vertical”'是否设置为你的ScrollView的子级(在LinearLayout的情况下)。

这是我的情况希望它可以帮助某人

.


c
chrisi1698

这里的所有答案对我来说(完全)都不起作用。回顾一下我们想要做的完整答案:我们有一个 ScrollView,据说填充了设备的视口,因此我们在布局 xml 中将 fillViewport 设置为“true”。然后,在 ScrollView 内部,我们有一个包含其他所有内容的 LinearLayout,LinearLayout 应该至少与其父 ScrollView 一样高,所以应该在底部的东西(LinearLayout)实际上是我们想要的,在屏幕底部(或在 ScrollView 的底部,以防 LinearLayout 的内容比屏幕高。

示例 activity_main.xml 布局:

<ScrollView
    android:id="@+id/layout_scrollwrapper"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:fillViewport="true"
    android:layout_alignParentTop="true"
    android:layout_above="@+id/layout_footer"
    >
<LinearLayout
    android:id="@+id/layout_scrollwrapper_inner"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    >
...content which might or might not be higher than screen height...
</LinearLayout>
</ScrollView>

然后,在 Activity 的 onCreate 中,我们“等待”LinearLayout 的布局完成(意味着它的父级布局也已经完成),然后将它的最小高度设置为 ScrollView 的高度。因此,它也适用于 ScrollView 不占据整个屏幕高度的情况。无论您在 ScrollView 还是内部 LinearLayout 上调用 .post(...) 都不会产生太大的影响,如果一个不适合您,请尝试另一个。

public class MainActivity extends AppCompatActivity {

@Override // Activity
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    LinearLayout linearLayoutWrapper = findViewById(R.id.layout_scrollwrapper_inner);

    ...

    linearLayoutWrapper.post(() -> {
        linearLayoutWrapper.setMinimumHeight(((ScrollView)linearLayoutWrapper.getParent()).getHeight());
    });
}
...
}

可悲的是,这不是一个仅限 xml 的解决方案,但它对我来说已经足够好了,希望它也能帮助其他一些受折磨的 android 开发人员在互联网上寻找解决这个问题的方法;D