Android LinearLayout 不在 ScrollView 内扩展

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2599837/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 06:30:32  来源:igfitidea点击:

LinearLayout not expanding inside a ScrollView

androidandroid-layout

提问by Felix

I have a LinearLayoutinside a ScrollViewthat has android:layout_height="fill_parent", but it doesn't expand to the full height of the ScrollView. My layout looks something like:

我有一个LinearLayout里面ScrollViewandroid:layout_height="fill_parent",但它并没有扩展到的最大高度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

I can see that the LinearLayoutdoesn't expand the full height of the ScrollViewbecause in Eclipse in Android Layout Editor, if I select the ScrollView(in the Outline panel) it is highlighted with a red border that fills the screen to the bottom but when I select the LinearLayoutits highlight doesn't expand to the bottom of the screen. How can I get it to do so?

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

The effect I'm trying to achieve is to have some text and a button below it (inside the LinearLayoutin level 4 there's just a button). The text can be big enough to need a scrollbar, in which case I want the user to have to scroll down in order to see the button. In case the text is not big enough for a scroll bar, I want the LinearLayoutcontaining the button to stick to the bottom of the screen.

我想要达到的效果是在它下面有一些文本和一个按钮(在第LinearLayout4 级里面只有一个按钮)。文本可以大到需要滚动条,在这种情况下,我希望用户必须向下滚动才能看到按钮。如果文本对于滚动条来说不够大,我希望LinearLayout包含按钮的按钮粘在屏幕底部。

At first I thought I shouldn't post the full XML because it's usually a turn-down to see a huge chunk of code in a question. However, it seems it might be necessary, so here's the full layout.

起初我认为我不应该发布完整的 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>

At the moment I have resorted to android:layout_gravity="bottom"on the problematic LinearLayout, which makes the button stick to the bottom of the screen no matter what. But that also makes the text stick to the bottom of the screen, which is not exactly what I was after.

目前我已经求助于android:layout_gravity="bottom"有问题的LinearLayout,这使得按钮无论如何都粘在屏幕底部。但这也使文本粘在屏幕底部,这并不是我所追求的。

Update:scratch that, android:layout_gravity="bottom"makes the ScrollViewunable to, well, scroll. Other ideas?

更新:从头开始,android:layout_gravity="bottom"使ScrollView无法滚动。其他想法?

回答by Felix

Found the solution myself in the end. The problem was not with the LinearLayout, but with the ScrollView(seems weird, considering the fact that the ScrollViewwasexpanding, while the LinearLayoutwasn't).

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

The solution was to use android:fillViewport="true"on the ScrollView.

解决方案是android:fillViewport="true"ScrollView.

回答by Hardik4560

I know this post is very old, For those who don't want to use android:fillViewport="true"because it sometimes doesn't bring up the edittext above keyboard. Use Relative layout instead of LinearLayout it solves the purpose.

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

回答by snctln

Can you provide your layout xml? Doing so would allow people to recreate the issue with minimal effort.

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

You might have to set

你可能需要设置

android:layout_weight="1"

for the item that you want expanded

对于您要扩展的项目

回答by Khushboo Aggarwal

The solution is to use

解决方案是使用

android:fillViewport="true"

android:fillViewport="true"

on Scroll viewand moreover try to use

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

"wrap_content"instead of "fill_parent"as "fill_parent"

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

is deprecated now.

现在已弃用。

回答by Braj

I have dynamically used to get this . I have a LinearLayout and within this used ScrollView as a child. Then i take again LinearLayout and add what ever View u want to this LinearLayout and then this LinearLayout add to ScrollView and finally add this ScrollView to LinearLayout. Then u can get scroll in ur ScrollView and nothing will left to visible.

我已经动态地用来得到这个。我有一个 LinearLayout 并且在这个使用 ScrollView 作为一个孩子。然后我再次使用 LinearLayout 并将您想要的视图添加到此 LinearLayout,然后将此 LinearLayout 添加到 ScrollView,最后将此 ScrollView 添加到 LinearLayout。然后你可以在你的 ScrollView 中滚动,没有任何东西是可见的。

LinearLayout(Parent)--ScrollView(child of LinerLayout) -- LinearLayout(child of ScrollView)-- add here textView, Buttons , spinner etc whatever u want . Then add this LinearLyout to ScrollView. Bcoz only one CHILD for ScrollView applicable and last add this ScrollView to LinearLyout.If defined area is exceed from Screen size then u will get a Scroll within ScrollView.

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

回答by Vishal Pandey

In my case i haven't given the

就我而言,我没有给出

orientation of LinearLayout(ScrollView's Child)

LinearLayout 的方向(ScrollView 的 Child)

So by default it takes horizontal, but scrollview scrols vertically, so please check if 'android:orientation="vertical"' is set to your ScrollView's Child(In the case of LinearLayout).

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

This was my case hopes it helps somebody

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

.

.