代码如下
- <?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>
左边实际效果,右边为目的效果
这个问题还是比较适合通过weight处理。
@张炎Lior的大体思路是正确的,如果你仅仅想让输入框那一行在下面,并且宽度随意增加,或者仅仅想让按钮显示出来,那么就不要设置weight比例,直接就是1:0就好了,具体代码见下面: 权重值越小越先渲染
- <?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="welcome"
- android:id="@+id/TextView"
- android:layout_width="fill_parent"
- android:layout_height="0dip"
- android:layout_weight="1">
- </TextView>
-
- <LinearLayout android:id="@+id/LinearLayout"
- android:orientation="horizontal"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_weight="0">
-
- <EditText android:id="@+id/EditText"
- android:layout_width="0dip"
- android:layout_height="wrap_content"
- android:layout_weight="1">
- </EditText>
-
- <Button android:text="label_submit_button"
- android:id="@+id/Button"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_weight="0">
- </Button>
-
- </LinearLayout>
-
- </LinearLayout>
有好几种方式,我提供一种通过设置weight的方式吧。请看代码:
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:orientation="vertical" >
-
- <TextView
- android:id="@+id/TextView"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:layout_weight="1"
- android:text="@string/hello" >
- </TextView>
-
- <LinearLayout
- android:id="@+id/LinearLayout"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:layout_gravity="bottom"
- android:layout_weight="8"
- android:orientation="horizontal"
- >
-
- <EditText
- android:id="@+id/EditText"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_weight="1" >
- </EditText>
-
- <Button
- android:id="@+id/Button"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="test"
- android:gravity="center"
- android:layout_weight="7" >
- </Button>
- </LinearLayout>
- </LinearLayout>
@张炎Lior方法不错。我也找到一种,用Relative Layout,属性设置成android:layout_alignParentBottom="true"
- <?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>
用RelativeLayout还有个好处是可以减少View的嵌套, 消除多余的递归.
要实现上面的效果可以直接去掉中间那层嵌套