我有一个ScrollView
里面包含一个LinearLayout
,LinearLayout
设置了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
我发现在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"
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"
,这使得按钮无论如何都会停留在屏幕底部。但是这也使得文本粘在屏幕底部,这并不完全是我想要的。
更新: 取消之前的操作,android:layout_gravity="bottom"
会使ScrollView
无法滚动。还有其他的想法吗?
android:weight
消耗额外空间的控件,否则这可能不会在布局中得到反映。 - Paul Lammertsma