我有一个线性布局内的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无法,好,滚动。其他的想法吗?


当前回答

All the answers here didn't work (completely) for me. Just to recap what we wanna do for a complete answer: We have a ScrollView, supposedly filling the device's viewport, thus we set fillViewport to "true" in the layout xml. Then, inside the ScrollView, we have a LinearLayout containing everything else, and that LinearLayout should be at least as high as its parent ScrollView, so stuff that's supposed to be on the bottom (of the LinearLayout) is actually, as we want it, at the bottom of the screen (or at the bottom of the ScrollView, in case the LinearLayout's content has more hight than the screen.

示例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>

然后,在活动的onCreate中,我们“等待”LinearLayout的布局完成(暗示它的父布局也已经完成),然后将它的最小高度设置为ScrollView的高度。因此,在ScrollView没有占据整个屏幕高度的情况下,它也可以工作。 无论你在ScrollView上调用.post(…)还是在内部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开发人员在互联网上搜索这个问题的解决方案

其他回答

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

你可能需要设置

android:layout_weight="1"

用于要展开的项

All the answers here didn't work (completely) for me. Just to recap what we wanna do for a complete answer: We have a ScrollView, supposedly filling the device's viewport, thus we set fillViewport to "true" in the layout xml. Then, inside the ScrollView, we have a LinearLayout containing everything else, and that LinearLayout should be at least as high as its parent ScrollView, so stuff that's supposed to be on the bottom (of the LinearLayout) is actually, as we want it, at the bottom of the screen (or at the bottom of the ScrollView, in case the LinearLayout's content has more hight than the screen.

示例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>

然后,在活动的onCreate中,我们“等待”LinearLayout的布局完成(暗示它的父布局也已经完成),然后将它的最小高度设置为ScrollView的高度。因此,在ScrollView没有占据整个屏幕高度的情况下,它也可以工作。 无论你在ScrollView上调用.post(…)还是在内部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开发人员在互联网上搜索这个问题的解决方案

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

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

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

在我的情况下,我没有给

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

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

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

.