详解Android中gravity和weight属性

来源:互联网 发布:小米网络电视怎么样 编辑:程序博客网 时间:2024/06/07 01:26
有点忘记这两个属性了,复习一下。

来看这个布局文件
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: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>  
[xml] view plaincopyprint?
  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文件。
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>  
[xml] view plaincopyprint?
  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文件做如下修改
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>  
[xml] view plaincopyprint?
  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开发环境搭建所需资源、安装和配置步骤图解       Android海量源码、教程与资料  

数字电视接口规范大全(行业标准)                     程序员必备:英语口语学习教程大全 

20本Linux电子书学习教程                   2013年25家IT高科技公司薪酬排行榜  

2013年Android平台8大预测                  优秀Android开发人员必须注意的10个误区 

Android开发应该掌握的28大内容和10大建议      成为“Android高手”的6个境界 

90后就业“钱景”分析:IT行业仍是最佳选择      90后就业“钱景”:IT仍是最佳选择

 


 

 

原创粉丝点击