LinearLayout 不在 ScrollView 内扩展 [英] LinearLayout not expanding inside a ScrollView

查看:24
本文介绍了LinearLayout 不在 ScrollView 内扩展的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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(在 Outline 面板中)它用填充屏幕底部的红色边框突出显示,但是当我选择 LinearLayout 时,它的突出显示不会扩展到屏幕底部.我怎样才能让它这样做?

I can see that the LinearLayout doesn't expand the full height of the ScrollView because 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 LinearLayout its highlight doesn't expand to the bottom of the screen. How can I get it to do so?

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

The effect I'm trying to achieve is to have some text and a button below it (inside the LinearLayout in 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 LinearLayout containing the button to stick to the bottom of the screen.

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

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 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",这使得按钮无论如何都粘在屏幕底部.但这也使文本粘在屏幕底部,这不是我想要的.

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" 使 ScrollView 无法滚动.其他想法?

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

推荐答案

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

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 ScrollView was expanding, while the LinearLayout wasn't).

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

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

这篇关于LinearLayout 不在 ScrollView 内扩展的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆