有点忘记这两个属性了,复习一下。

来看这个布局文件

  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. >
  7. <EditText
  8. android:layout_width="fill_parent"
  9. android:layout_height="wrap_content"
  10. android:text="one"/>
  11. <EditText
  12. android:layout_width="fill_parent"
  13. android:layout_height="wrap_content"
  14. android:text="two"/>
  15. <EditText
  16. android:layout_width="fill_parent"
  17. android:layout_height="wrap_content"
  18. android:text="three"/>
  19. </LinearLayout>

这是一个很正常的布局文件了,效果如下。

当我们给这三个EditText设置上gravity属性之后效果如下,先看 xml文件。

  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. >
  7. <EditText
  8. android:layout_width="fill_parent"
  9. android:layout_height="wrap_content"
  10. android:gravity="left"
  11. android:text="one"/>
  12. <EditText
  13. android:layout_width="fill_parent"
  14. android:layout_height="wrap_content"
  15. android:gravity="center"
  16. android:text="two"/>
  17. <EditText
  18. android:layout_width="fill_parent"
  19. android:layout_height="wrap_content"
  20. android:gravity="right"
  21. android:text="three"/>
  22. </LinearLayout>

由此可以看出gravity属性是用来控制 EditText里边文本的位置。

我们现在对xml文件做如下修改

  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. >
  7. <EditText
  8. android:layout_width="fill_parent"
  9. android:layout_height="wrap_content"
  10. android:gravity="left"
  11. android:text="one"/>
  12. <EditText
  13. android:layout_width="fill_parent"
  14. android:layout_height="wrap_content"
  15. android:gravity="center"
  16. android:layout_weight="1.0"
  17. android:text="two"/>
  18. <EditText
  19. android:layout_width="fill_parent"
  20. android:layout_height="wrap_content"
  21. android:gravity="right"
  22. android:text="three"/>
  23. </LinearLayout>

运行效果如下

感觉上这个android:layout_weight="1.0"是用来设置控件的大小,因为经过我们的设置中间的那个EditText变大了。其它两个EditText的 android:layout_weight属性我们没有设置,没有设置就会使用默认值, 默认值为 0.0。 设置为1.0的那个控件会填充剩余空白的部分。 
我们如果想要3个组件均等地共享空间,应该将他们的weight属性都设置为 1.0,这样将均匀的扩展每一个EditText。 
    

android:gravity是用来设置控件里面的子控件等的对齐方式的 
android:layout_gravity是用来设置控件本身在它的父控件中的对齐方式!!!!

from:http://byandby.iteye.com/blog/1020374

最新文章

  1. GitHub实战系列~4.把github里面的库克隆到指定目录+日常使用 2015-12-11
  2. CSS3与页面布局学习笔记(五)——Web Font与CSS Sprites(又称CSS精灵、雪碧图)技术
  3. Nagios配置文件详解
  4. Linux - 进程状态
  5. android 项目中规范使用SharedPreferences
  6. javascript创建对象的方法总结
  7. 【转】Deprecated: Function ereg_replace() is deprecated的解决方法
  8. Maven的安装
  9. noip2015day1 T1 4510 神奇的幻方
  10. 在Eclipse中导入文件和文件夹
  11. Django 学习笔记之六 建立一个简单的博客应用程序
  12. C#解leetcode 228. Summary Ranges Easy
  13. 类似NL的update更新
  14. 优盘(U 盘) 采用TLC, MLC, SLC芯片 的区别 与使用寿命
  15. 关于ajax的短轮询问题
  16. Windows 10 IoT Core环境配置中的那些坑
  17. 》》豆瓣API
  18. 浅析设备管理的MTTR,MTTF,MTBF计算方法
  19. UNIX网络编程——原始套接字的魔力【续】
  20. [转帖]IP /TCP协议及握手过程和数据包格式中级详解

热门文章

  1. php 数据库查询order by 与查询返回的数据类型
  2. 记录CentOS7.X版本下安装MySQL5.7数据库
  3. Win7 IIS 局域网中无法访问网页
  4. javascript中typeof与instanceof的区别
  5. cpp 学习笔记
  6. (转)Linux curl命令详解
  7. Kure讲HTML_如何学习HTML
  8. http学习总结及思考
  9. HDU 5441——Travel——————【并查集+二分查界限】
  10. html:option