OGeek|极客世界-中国程序员成长平台

标题: android - LinearLayout 未在 ScrollView 内扩展 [打印本页]

作者: 菜鸟教程小白    时间: 2022-8-1 01:21
标题: android - LinearLayout 未在 ScrollView 内扩展

我有一个 LinearLayoutScrollView 内有 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

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

我想要达到的效果是在其下方有一些文本和一个按钮(在第 4 级的 LinearLayout 内只有一个按钮)。文本可以大到需要滚动条,在这种情况下,我希望用户必须向下滚动才能看到按钮。如果文本不足以容纳滚动条,我想要 LinearLayout包含粘贴到屏幕底部的按钮。

起初我认为我不应该发布完整的 XML,因为在一个问题中看到大量代码通常是拒绝的。但是,这似乎是必要的,所以这里是完整的布局。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    androidrientation="vertical"
    xmlns:android="http://schemas.android.com/apk/res/android">
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        androidrientation="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"
            androidrientation="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"
                androidrientation="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"关于有问题的LinearLayout ,这使得按钮无论如何都粘在屏幕底部。但这也使文本粘在屏幕底部,这并不是我想要的。

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



Best Answer-推荐答案


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

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

关于android - LinearLayout 未在 ScrollView 内扩展,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2599837/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://jike.in/) Powered by Discuz! X3.4