我有一个线性布局内的ScrollView有android: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

我可以看到线性布局并没有展开ScrollView的全部高度,因为在Eclipse中的Android布局编辑器中,如果我选择ScrollView(在大纲面板中),它会突出显示一个红色边框,填充屏幕底部,但当我选择线性布局时,它的高亮并不会扩展到屏幕底部。我怎样才能让它这样做呢?

我想要达到的效果是有一些文本和一个按钮在它下面(在第4级的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>

目前,我已经诉诸于android:layout_gravity="bottom"在有问题的线性布局,这使得按钮粘在屏幕的底部无论什么。但这也会使文本粘在屏幕底部,这并不是我想要的。

更新:划痕,android:layout_gravity="bottom"使ScrollView无法,好,滚动。其他的想法吗?


当前回答

解决办法就是使用

android:fillViewport=“true”

在滚动视图,而且尝试使用

"wrap_content"代替"fill_parent"作为"fill_parent"

现在已弃用。

其他回答

在我的情况下,我没有给

线性布局的方向(ScrollView的子)

所以默认情况下,它采取水平,但scrollview滚动垂直,所以请检查如果'android:orientation="vertical"'被设置为你的scrollview的孩子(在线性布局的情况下)。

这是我的案子希望能帮到别人

.

动态地得到这个。我有一个线性布局,其中使用ScrollView作为子。然后我再次采取线性布局,并添加任何视图,你想要这个线性布局,然后这个线性布局添加到ScrollView,最后添加这个ScrollView到线性布局。然后你可以滚动你的ScrollView,什么都不会留下可见。

线性布局(父)—ScrollView(孩子的LinerLayout)—线性布局(孩子的ScrollView)—在这里添加textView,按钮,旋转器等任何你想要的。然后将这个LinearLyout添加到ScrollView。因为ScrollView只有一个CHILD适用,最后添加这个ScrollView到LinearLyout。如果定义的区域超出了屏幕大小,那么你将在ScrollView中获得一个滚动。

解决办法就是使用

android:fillViewport=“true”

在滚动视图,而且尝试使用

"wrap_content"代替"fill_parent"作为"fill_parent"

现在已弃用。

我知道这篇文章很旧了,对于那些不想使用android:fillViewport="true"的人,因为它有时不会弹出键盘上方的编辑文本。 使用相对布局而不是线性布局,它解决了这个问题。

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

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