android如何让控件摆放在屏幕底部

代码如下

         
         
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical"
  4. android:layout_width="fill_parent"
  5. android:layout_height="fill_parent">

  6. <TextView android:text="@string/welcome"
  7. android:id="@+id/TextView"
  8. android:layout_width="fill_parent"
  9. android:layout_height="wrap_content">
  10. </TextView>

  11. <LinearLayout android:id="@+id/LinearLayout"
  12. android:orientation="horizontal"
  13. android:layout_width="wrap_content"
  14. android:layout_height="wrap_content"
  15. android:gravity="bottom">

  16. <EditText android:id="@+id/EditText"
  17. android:layout_width="fill_parent"
  18. android:layout_height="wrap_content">
  19. </EditText>

  20. <Button android:text="@string/label_submit_button"
  21. android:id="@+id/Button"
  22. android:layout_width="wrap_content"
  23. android:layout_height="wrap_content">
  24. </Button>

  25. </LinearLayout>

  26. </LinearLayout>

左边实际效果,右边为目的效果
点击查看原始尺寸

这个问题还是比较适合通过weight处理。
@张炎Lior的大体思路是正确的,如果你仅仅想让输入框那一行在下面,并且宽度随意增加,或者仅仅想让按钮显示出来,那么就不要设置weight比例,直接就是1:0就好了,具体代码见下面:
权重值越小越先渲染

       
       
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical"
  4. android:layout_width="fill_parent"
  5. android:layout_height="fill_parent">
  6. <TextView android:text="welcome"
  7. android:id="@+id/TextView"
  8. android:layout_width="fill_parent"
  9. android:layout_height="0dip"
  10. android:layout_weight="1">
  11. </TextView>
  12. <LinearLayout android:id="@+id/LinearLayout"
  13. android:orientation="horizontal"
  14. android:layout_width="fill_parent"
  15. android:layout_height="wrap_content"
  16. android:layout_weight="0">
  17. <EditText android:id="@+id/EditText"
  18. android:layout_width="0dip"
  19. android:layout_height="wrap_content"
  20. android:layout_weight="1">
  21. </EditText>
  22. <Button android:text="label_submit_button"
  23. android:id="@+id/Button"
  24. android:layout_width="wrap_content"
  25. android:layout_height="wrap_content"
  26. android:layout_weight="0">
  27. </Button>
  28. </LinearLayout>
  29. </LinearLayout>
  • 张炎Lior
    50

有好几种方式,我提供一种通过设置weight的方式吧。请看代码:

       
       
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="fill_parent"
  4. android:layout_height="fill_parent"
  5. android:orientation="vertical" >
  6. <TextView
  7. android:id="@+id/TextView"
  8. android:layout_width="fill_parent"
  9. android:layout_height="fill_parent"
  10. android:layout_weight="1"
  11. android:text="@string/hello" >
  12. </TextView>
  13. <LinearLayout
  14. android:id="@+id/LinearLayout"
  15. android:layout_width="fill_parent"
  16. android:layout_height="fill_parent"
  17. android:layout_gravity="bottom"
  18. android:layout_weight="8"
  19. android:orientation="horizontal"
  20. >
  21. <EditText
  22. android:id="@+id/EditText"
  23. android:layout_width="fill_parent"
  24. android:layout_height="wrap_content"
  25. android:layout_weight="1" >
  26. </EditText>
  27. <Button
  28. android:id="@+id/Button"
  29. android:layout_width="fill_parent"
  30. android:layout_height="wrap_content"
  31. android:text="test"
  32. android:gravity="center"
  33. android:layout_weight="7" >
  34. </Button>
  35. </LinearLayout>
  36. </LinearLayout>

@张炎Lior方法不错。我也找到一种,用Relative Layout,属性设置成android:layout_alignParentBottom="true"

       
       
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout
  3. xmlns:android="http://schemas.android.com/apk/res/android"
  4. android:layout_width="fill_parent"
  5. android:layout_height="fill_parent">
  6. <TextView
  7. android:text="@string/welcome"
  8. android:id="@+id/TextView"
  9. android:layout_width="fill_parent"
  10. android:layout_height="wrap_content"
  11. android:layout_alignParentTop="true">
  12. </TextView>
  13. <RelativeLayout
  14. android:id="@+id/InnerRelativeLayout"
  15. android:layout_width="wrap_content"
  16. android:layout_height="wrap_content"
  17. android:layout_alignParentBottom="true" >
  18. <Button
  19. android:text="@string/label_submit_button"
  20. android:id="@+id/Button"
  21. android:layout_alignParentRight="true"
  22. android:layout_width="wrap_content"
  23. android:layout_height="wrap_content">
  24. </Button>
  25. <EditText
  26. android:id="@+id/EditText"
  27. android:layout_width="fill_parent"
  28. android:layout_toLeftOf="@id/Button"
  29. android:layout_height="wrap_content">
  30. </EditText>
  31. </RelativeLayout>
  32. </RelativeLayout>
  • 1
    支持用RelativeLayout ,
    用RelativeLayout还有个好处是可以减少View的嵌套, 消除多余的递归.
    要实现上面的效果可以直接去掉中间那层嵌套
     

你的代码的地17行改为

       
       
  1. android:layout_gravity="bottom">

可以达到,EditText和Button在最下边显示;

在<EditText/>中加入一行

       
       
  1. android:layout_weight="1"

可以让你的EditText最大显示

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值