Android 如何使控件位于界面底部
使控件位于界面底部有很多種辦法,下面我就分情況說一下: 一、LinearLayout布局中: 把握三個原則即可輕松使控件位于界面底部: 1、最外層父容器LinearLayout設定高度 layout_height="match_parent" 2、 内層LinearLayout設定 layout_weight="1" 并且 layout_height="0dp" 3、内層LinearLayout 設定其中控件位置:android:gravity="center|bottom"
![](https://img.laitimes.com/img/9ZDMuAjOiMmIsIjOiQnIsIiclRnblN0LclHdpZXYyd2LcBzNvwVZ2x2bzNXak9CX90TQNNkRrFlQKBTSvwFbslmZvwFMwQzLcVmepNHdu9mZvwFVywUNMZTY18CX052bm9CX90TUOFTVE9kenRUT4FEVkZXUYpVd1kmYr50MZV3YyI2cKJDT29GRjBjUIF2LcRHelR3LcJzLctmch1mclRXY39TM3YzMyEDM5ETOyITM0EDMy8CX0Vmbu4GZzNmLn9Gbi1yZtl2Lc9CX6MHc0RHaiojIsJye.jpg)
下面用實際例子來驗證(如上圖)。 布局檔案代碼如下: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent" android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin" android:orientation="vertical" tools:context=".MainActivity">
<TextView android:text="@string/hello_world" android:layout_weight="0" android:layout_width="wrap_content" android:layout_height="wrap_content" />
<LinearLayout android:layout_width="fill_parent" android:layout_height="0dp" android:orientation="horizontal" android:gravity="bottom|center" android:layout_weight="1"> <EditText android:minWidth="280dp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="請輸入内容"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="發送"/> </LinearLayout> </LinearLayout> 注意:使用LinearLayout布局時不要忘了設定控件的排序方式哦 android:orientation 這個屬性不設定會報錯哦 二、RelativeLayout布局中 這種布局方式比較靈活,設定起來也友善,隻需要在控件中添加
android:alignParentBottom
這個屬性即可讓控件位于螢幕下方。