来看这个布局文件
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
- <EditText
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="one"/>
- <EditText
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="two"/>
- <EditText
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="three"/>
- </LinearLayout>
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <EditText android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="one"/> <EditText android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="two"/> <EditText android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="three"/> </LinearLayout>
这是一个很正常的布局文件了,效果如下。
![](http://dl.iteye.com/upload/attachment/471921/ca320a5e-2806-34db-ae76-e7aa8d983575.jpg)
当我们给这三个EditText设置上gravity属性之后效果如下,先看 xml文件。
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
- <EditText
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:gravity="left"
- android:text="one"/>
- <EditText
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:gravity="center"
- android:text="two"/>
- <EditText
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:gravity="right"
- android:text="three"/>
- </LinearLayout>
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <EditText android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="left" android:text="one"/> <EditText android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center" android:text="two"/> <EditText android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="right" android:text="three"/> </LinearLayout>
![](http://dl.iteye.com/upload/attachment/471928/d54f05ad-8133-319f-a383-f6764b28f0e1.jpg)
由此可以看出gravity属性是用来控制 EditText里边文本的位置。
我们现在对xml文件做如下修改
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
- <EditText
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:gravity="left"
- android:text="one"/>
- <EditText
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:gravity="center"
- android:layout_weight="1.0"
- android:text="two"/>
- <EditText
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:gravity="right"
- android:text="three"/>
- </LinearLayout>
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <EditText android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="left" android:text="one"/> <EditText android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center" android:layout_weight="1.0" android:text="two"/> <EditText android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="right" android:text="three"/> </LinearLayout>
运行效果如下
感觉上这个android:layout_weight="1.0"是用来设置控件的大小,因为经过我们的设置中间的那个EditText变大了。其它两个EditText的 android:layout_weight属性我们没有设置,没有设置就会使用默认值, 默认值为 0.0。 设置为1.0的那个控件会填充剩余空白的部分。
我们如果想要3个组件均等地共享空间,应该将他们的weight属性都设置为 1.0,这样将均匀的扩展每一个EditText。
---------------------------------------------------------------------------------------------------------------------
2011年3月最新“985”工程大学名单(39所) 2011年3月最新“211”工程大学名单(112所)
10类最急需IT人才:Java开发者居首 海量Java教程、学习资料和源码
2012年度中国工科大学排名100强名单 特色985大学高校名单
2012年中国大学高校排行榜--580强排名 中国最高等级实验室—“国家实验室”名单
长时间操作电脑需要养成的几个好习惯 90后就业“钱景”:IT仍是最佳选择
2012年度世界500强企业,中国大陆香港台湾公司上榜名单(79家)
2012年全球城市竞争力排名500强名单,69个中国城市上榜
2012四月发布的世界各国人均GDP排名名单,中国超泰国 2012年全国各省平均工资排行
2011-2012年度中国软件企业公司100强排行榜名单 世界各国国土面积和人口排名