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

来看这个布局文件

  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. java——哈希表 HashTable

    在一个类中重写hashCode()和equals() package Date_pacage.hash; public class Student { private int grade; priva ...

  2. 转 Comparison of Red Hat and Oracle Linux kernel versions and release strings

    Originally derived from Red Hat Enterprise Linux (RHEL), Oracle Linux (OL) contains minor difference ...

  3. Typora中给代码块设置快捷键

    Tpyore中大部分的操作都是有快捷键的.但是有那么几个常用的却没有快捷键.就比如代码块,这个常用的操作,还有有序无需列表. 下边教会你怎么设置快捷键,打开设置,Preferences[偏好设置],然 ...

  4. JavaSE---线程同步

    1.当多个线程同时访问同一个数据时,容易出现线程安全问题,必须进行线程同步: 2.解决方案: 1.1 Java的多线程引入了  同步监视器  ,使用同步监视器的通用方法就是  同步代码块 //线程开始 ...

  5. 可输入的 Combox(DropDownList)

    aspx页面中需要可以输入的combox,在网上找了一个js的插件,效果图如下:

  6. DevStack添加Swift

    # Swift# ----- # Swift is now used as the back-end for the S3-like object store. If Nova's# objectst ...

  7. 会话状态在此上下文中不可用HttpModule中无法访问Session原因

    写了一个自定义HttpModule,但始终访问不了Session,代码如下: public class RouteModule : IHttpModule, System.Web.SessionSta ...

  8. OnDeserializedAttribute 不能作用于 Xml Serialization 上

    在做测试的时候习惯用xml serialization观察结果.想当然的认为OnDeserialized Attribute 可以同样的使用,但是其实Xml Serialization 并没有实现相对 ...

  9. [Java][Liferay] 模拟用户

    以admin的帐号登陆 Navigation to Users and Organizations -> All Users 找到你要查看的user,点击Actions->Imperson ...

  10. Swift UI开发初探 (转)

    原文地址:http://www.tairan.com/archives/6600 关于Swift语法,可以参考<Apple Swift编程语言入门教程> 效果如下: 开发环境 Xcode6 ...