Android 使控件位于界面底部

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

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值