Android 使控件坐落界面底部

Android 使控件位于界面底部

Android 如何使控件位于界面底部

    使控件位于界面底部有很多种办法,下面我就分情况说一下:
  一、LinearLayout布局中:
         把握三个原则即可轻松使控件位于界面底部:
          1、最外层父容器LinearLayout设置高度  layout_height="match_parent"
             2、 内层LinearLayout设置    layout_weight="1" 并且 layout_height="0dp"
             3、内层LinearLayout 设置其中控件位置:android:gravity="center|bottom"
           Android 使控件坐落界面底部
下面用实际例子来验证(如上图)。
    布局文件代码如下:
                
                       <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 
              这个属性即可让控件位于屏幕下方。