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

http://www.dewen.org/q/1800

代码如下


  1. xml version="1.0" encoding="utf-8"?>
  2. 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. android:text="@string/welcome"
  7. android:id="@+id/TextView"
  8. android:layout_width="fill_parent"
  9. android:layout_height="wrap_content">
  10. android:id="@+id/LinearLayout"
  11. android:orientation="horizontal"
  12. android:layout_width="wrap_content"
  13. android:layout_height="wrap_content"
  14. android:gravity="bottom">
  15. android:id="@+id/EditText"
  16. android:layout_width="fill_parent"
  17. android:layout_height="wrap_content">
  18. android:text="@string/label_submit_button"
  19. android:id="@+id/Button"
  20. android:layout_width="wrap_content"
  21. android:layout_height="wrap_content">

左边实际效果,右边为目的效果

如何修改?

评论 ( 1) •  分享 •  链接 •  2012-02-23 
  • 0
    这个问题,自己调一调显示结构不就知道了... –  frank_hust  2012-10-31
4个答案
票 数 
  • andev

    4 票

  • 5551

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


  1. xml version="1.0" encoding="utf-8"?>
  2. 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. 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. android:id="@+id/LinearLayout"
  12. android:orientation="horizontal"
  13. android:layout_width="fill_parent"
  14. android:layout_height="wrap_content"
  15. android:layout_weight="0">
  16. android:id="@+id/EditText"
  17. android:layout_width="0dip"
  18. android:layout_height="wrap_content"
  19. android:layout_weight="1">
  20. android:text="label_submit_button"
  21. android:id="@+id/Button"
  22. android:layout_width="wrap_content"
  23. android:layout_height="wrap_content"
  24. android:layout_weight="0">
评论 ( 0) •  链接 • 2012-02-25
  • 张炎Lior

    3 票

  • 509

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


  1. xml version="1.0" encoding="utf-8"?>
  2. 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. android:id="@+id/TextView"
  7. android:layout_width="fill_parent"
  8. android:layout_height="fill_parent"
  9. android:layout_weight="1"
  10. android:text="@string/hello" >
  11. android:id="@+id/LinearLayout"
  12. android:layout_width="fill_parent"
  13. android:layout_height="fill_parent"
  14. android:layout_gravity="bottom"
  15. android:layout_weight="8"
  16. android:orientation="horizontal"
  17. >
  18. android:id="@+id/EditText"
  19. android:layout_width="fill_parent"
  20. android:layout_height="wrap_content"
  21. android:layout_weight="1" >
  22. android:id="@+id/Button"
  23. android:layout_width="fill_parent"
  24. android:layout_height="wrap_content"
  25. android:text="test"
  26. android:gravity="center"
  27. android:layout_weight="7" >
小飞
6782
编辑于  2012-03-02
评论 ( 1) •  链接 • 2012-02-24
  • 0
    it works!! –  李剑波  2012-02-24
  • 李剑波

    2 票

  • 4768

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


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

    1 票

  • 48

你的代码的地17行改为


  1. android:layout_gravity="bottom">

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

中加入一行


  1. android:layout_weight="1"

可以让你的EditText最大显示

评论 ( 0) •  链接 • 2012-07-11

你可能感兴趣的:(Android)