首页 文章

LinearLayout不在ScrollView内扩展

提问于
浏览
343

ScrollView 里面 LinearLayoutandroid: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

我可以看到 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" ,这使得按钮无论如何都会粘在屏幕的底部 . 但这也使文本粘在屏幕的底部,这不是我所追求的 .

Update: 从头开始, android:layout_gravity="bottom" 使得 ScrollView 无法滚动 . 其他想法?

6 回答

  • 1

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

    你可能需要设置

    android:layout_weight="1"
    

    对于要扩展的项目

  • 17

    最后我自己找到了解决方案 . 问题不是 LinearLayout ,而是 ScrollView (看起来很奇怪,考虑到 ScrollView was 扩展,而 LinearLayout 不是) .

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

  • 914

    我知道这篇文章很老了,对于那些不想使用 android:fillViewport="true" 的人来说,因为它有时候不会调出键盘上方的edittext . 使用相对布局而不是LinearLayout它解决了目的 .

  • 8

    我动态地习惯了这个 . 我有一个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 .

  • 2

    在我的情况下,我还没有给出

    LinearLayout的方向(ScrollView的孩子)

    所以默认情况下它需要水平,但scrollview是垂直的,所以请检查'android:orientation =“vertical”'是否设置为ScrollView的Child(在LinearLayout的情况下) .

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

    .

  • 1

    解决方案是使用

    android:fillViewport="true"

    Scroll view ,而且尝试使用

    "wrap_content" 而不是 "fill_parent""fill_parent"

    现已弃用 .

相关问题