这是我的布局代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:text="@string/welcome"
android:id="@+id/TextView"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</TextView>
<LinearLayout android:id="@+id/LinearLayout"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="bottom">
<EditText android:id="@+id/EditText"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</EditText>
<Button android:text="@string/label_submit_button"
android:id="@+id/Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</Button>
</LinearLayout>
</LinearLayout>
比较容易的做法就是设置TextView充满整个高度,但是这会引起按钮没有空间或者是输入框。基本上这个问题是我想要submit button和text entry在底部是一个合适的高度,text view填满剩下的控件,同样的在这个水平线性布局里边,我想要submit button包含它的内容,text entry来包含剩下的空间。
如果在这个线性布局里的第一个项目是告知它就是用来填满这个布局的,没有给其他项目留空间,我在这个布局里如何得到一个首先是在这个线性布局里,填满除了最低要求的其他项目的项目?
编辑1:
Relative Layouts 确实是答案,谢谢。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:text="@string/welcome"
android:id="@+id/TextView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true">
</TextView>
<RelativeLayout
android:id="@+id/InnerRelativeLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true" >
<Button
android:text="@string/label_submit_button"
android:id="@+id/Button"
android:layout_alignParentRight="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</Button>
<EditText
android:id="@+id/EditText"
android:layout_width="fill_parent"
android:layout_toLeftOf="@id/Button"
android:layout_height="wrap_content">
</EditText>
</RelativeLayout>
</RelativeLayout>
我想你应该试试relative layout.
如果你使用相对布局来填充整个屏幕,你应该可以使用android:layout_alignParentBottom 来移动按钮到屏幕的底部。
如果你的屏幕底部的视图不是以相对布局来显示的,那么也许上边的布局将占据所有的空间。在这种情况下,在这个android布局:以上的布局中,在你的布局文件和剩下的布局位置,首先你应该把这个视图放在底部。这能够使按钮的视图占据尽可能多的像他需要的空间,剩下的布局可以填满剩下的所有屏幕。
在ScrollViet中这个不起作用,在页面底部,ScrollView中无论如何都会随着相对布局重叠,
我用了一个动态拉伸来固定 FrameLayout :
android:layout_height="fill_parent" android:layout_width="fill_parent"
android:fillViewport="true">
android:layout_width="fill_parent" android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical">
<!-- 内容都在这里 -->
<!-- 拉伸布局框架, 用layout_weight -->
<FrameLayout
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:layout_weight="1">
</FrameLayout>
<!-- 内容固定到屏幕底部 -->
<LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal">
<!-- 你底部的内容 -->
</LinearLayout>
</LinearLayout>
通过在线性布局中嵌套相对布局,你可以保持你的初始线性布局:
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:text="welcome"
android:id="@+id/TextView"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</TextView>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button android:text="submit"
android:id="@+id/Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true">
</Button>
<EditText android:id="@+id/EditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toLeftOf="@id/Button"
android:layout_alignParentBottom="true">
</EditText>
</RelativeLayout>
</LinearLayout>
Sueyexin的答案是相当正确的,但是我个人认为不是100%适合相对布局,所以我想要介绍一下“filler”,置空
TextView,像这样:
<!-- filler -->
<TextView android:layout_height="0dip"
android:layout_width="fill_parent"
android:layout_weight="1" />
在这个元素之前,应该就在屏幕底部了。