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

来看这个布局文件

  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

【转】android gravity属性 和 weight属性的更多相关文章

  1. android gravity属性 和 weight属性

    来看这个布局文件 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:and ...

  2. Android开发技巧一--weight属性实现视图的居中(半)显示

    面试时,一位面试官问到:“如果我想讲按钮居中显示,并且占据其父视图宽度的一半,应该怎么做到呢?”即实现这种效果: 我们使用weightSum属性和layout_weight属性实现这一要求: < ...

  3. android: android 布局中的weight 属性

    android: weight是线性布局的特有属性,控件的宽度和高度的不同,也会存在差异. 示例1:将宽度设置为包裹类型wrap_content或0dp <?xml version=" ...

  4. Android组件---四大布局的属性详解

    [声明] 欢迎转载,但请保留文章原始出处→_→ 文章来源:http://www.cnblogs.com/smyhvae/p/4372222.html Android常见布局有下面几种: LinearL ...

  5. Android Hack1 使用weight属性实现视图的居中显示

    本文地址:http://www.cnblogs.com/wuyudong/p/5898403.html,转载请注明源地址. 如果要实现如下图所示的将按钮居中显示,并且占据父视图的一半,无论屏幕是否旋转 ...

  6. Android之使用weight属性实现控件的按比例分配空间

    从今天開始,把看书时候的知识点整理成博客, 这个比較简单,预计有经验的都用过,weight属性 在做Android布局的时候,常常遇到须要几个控件按比例分配空间的情况 比方下图效果 在底部设置两个bu ...

  7. Android weight属性详解

    android:layout_weight是一个经常会用到的属性,它只在LinearLayout中生效,下面我们就来看一下: 当我们把组件宽度设置都为”match_parent”时: <Butt ...

  8. android:layout_gravity和android:gravity属性的区别(转)

    gravity的中文意思就是”重心“,就是表示view横向和纵向的停靠位置 android:gravity:是对view控件本身来说的,是用来设置view本身的文本应该显示在view的什么位置,默认值 ...

  9. android:layout_gravity和android:gravity属性的区别

    一.介绍: gravity的中文意思就是”重心“,就是表示view横向和纵向的停靠位置 (1).android:gravity:是对view控件本身来说的,是用来设置view本身的内容应该显示在vie ...

随机推荐

  1. c语言中有关0和1的运算问题

    /*有关0和1 的总结 最近做题总是混淆0 和 1 对于/ 和 %运算时候的结果怎么算 所以就上机试验了一番 结论: c语言中,0/任何数都为0 0%任何数都为0 1/任何数都为0 1%任何数都余1 ...

  2. Javascript各种事件汇总

    https://www.cnblogs.com/diligenceday/p/4190173.html#undefined https://www.cnblogs.com/starof/p/40663 ...

  3. PIXI 写一个字及图片保存(2)

    <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...

  4. python 运算符-赋值运算

    结果是值 算数运算: a = 10 * 10 赋值运算: a = a + 1  a  +=1  结果是布尔值 比较运算: a  = 1 > 5 逻辑运算: a =  1 > 6 or 1= ...

  5. GRE新东方推荐学习方法(2010年左右)

    单词:新东方新版红宝书(<NEW GRE 词汇精选>),不用<再要你命三个> 填空:新东方绿皮书(扎实的词汇量) 阅读:1 新东方绿皮书:2 <GRE阅读 39+3全攻略 ...

  6. Unity 在Inspector面板,脚本前面的激活对勾

    写个脚本,把它挂在一个游戏对象上: using System.Collections; using System.Collections.Generic; using UnityEngine; pub ...

  7. DRF-->1 序列化组件的使用和接口设计---get

    定义序列化器(本质就是一个类),一般包括模型类的字段,有自己的字段类型规则.实现了序列化器后,就可以创建序列化对象以及查询集进行序列化操作,通过序列化对象.data来获取数据(不用自己构造字典,再返回 ...

  8. Map集合练习之对字符串中字母出现的次数求和

    不多说,直接上干货! 代码需求 如有这么一个字符串 String str = "fdg+avAdc bs5dDa9c-dfs"; MapTest.java package zhou ...

  9. java多线程之守护线程与非守护线程

    在java线程中有两种线程,一种是用户线程,其余一种是守护线程. 守护线程具有特殊的含义,比如gc线程.当最后一个非守护线程执行完后,守护线程随着jvm一同结束工作. java中的守护线程需要将Dae ...

  10. Windows Store 应用中获取程序集版本号的方法

    本文为个人博客备份文章,原文地址: http://validvoid.net/windows-store-app-get-assembly-version/ WinRT 中对反射做了很多限制,假设 W ...