https://www.zybuluo.com/zzudhj/note/102067

在Android开发过程中,在编写布局文件经常会用layout_weight属性:从字面意思上看权重、比值、按比例。。。 
通过例子来看看layout_weight的用法 
example1: 
 
该布局有三部分组成,title、edit、bottom,其中,为了满足edit可以填充父窗口,可以为EditText添加layou_weight属性。通过Dump view UI hierarchy for Automatorfen 分析,得到结构如图: 
 
通过代码实现

 
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. android:layout_width="match_parent"
  3. android:layout_height="match_parent"
  4. android:orientation="vertical">
  5. <TextView
  6. android:layout_width="match_parent"
  7. android:layout_height="48dp"
  8. android:gravity="center"
  9. android:text="标题1" />
  10. <EditText
  11. android:layout_width="match_parent"
  12. android:layout_height="0dp"
  13. android:layout_weight="1" />
  14. <LinearLayout
  15. android:layout_width="match_parent"
  16. android:layout_height="wrap_content">
  17. <Button
  18. android:layout_width="0dp"
  19. android:layout_height="48dp"
  20. android:layout_weight="1"
  21. android:text="save" />
  22. <Button
  23. android:layout_width="0dp"
  24. android:layout_height="48dp"
  25. android:layout_weight="1"
  26. android:text="cancel" />
  27. </LinearLayout>
  28. </LinearLayout>

有了初步的认识,就来看看经常会遇到情景 
example2: 

 
  1. <TextView
  2. android:layout_width="0dp"
  3. android:layout_height="match_parent"
  4. android:text="textview1"
  5. android:background="#ee0000"
  6. android:layout_weight="1"/>
  7. <TextView
  8. android:layout_width="0dp"
  9. android:layout_height="match_parent"
  10. android:text="textview2"
  11. android:background="#eeee00"
  12. android:layout_weight="2"/>
  13. <TextView
  14. android:layout_width="0dp"
  15. android:layout_height="match_parent"
  16. android:text="textview3"
  17. android:background="#ee0ee0"
  18. android:layout_weight="2"/>

example3: 

 
  1. <TextView
  2. android:layout_width="match_parent"
  3. android:layout_height="match_parent"
  4. android:text="textview1"
  5. android:background="#ee0000"
  6. android:layout_weight="1"/>
  7. <TextView
  8. android:layout_width="match_parent"
  9. android:layout_height="match_parent"
  10. android:text="textview2"
  11. android:background="#eeee00"
  12. android:layout_weight="2"/>
  13. <TextView
  14. android:layout_width="match_parent"
  15. android:layout_height="match_parent"
  16. android:text="textview3"
  17. android:background="#ee0ee0"
  18. android:layout_weight="2"/>

其中,example2与example3区别

 
  1. <TextView
  2. android:layout_width="xxx"
  3. ...
  4. />

分析可以知道layout_weight表示:控件长度= 原来长度(Length) + 屏幕剩余(SLength)* weight_i,其中屏幕长度为L 
example2中: 
Length = 0, 
SLength= L, 
textview1.weight = 1/5 
textview1.length = 1/5L; 
同理 textview2.length = 2/5L 
textview3.length = 2/5L 
example3中: 
Length= L, 
SLength = -2L, 
textview1.length = L + (- 2/5 L) = 3/5L 
textview2.length = L + (- 4/5 L) = 1/5L 
textview3.length = L + (- 4/5 L) = 1/5L

ps: 
textview1.weight =1 
textview2.weight =2 
textview3.weight =3 
如果这样设置layout_weight的值,当 android:layout_width="match_parent"的情景下, 
你会发现 
textview1.length = L + (- 2/6 L) = 2/3L 
textview2.length = L + (- 4/6 L) = 1/3L 
textview3 不见啦啦啦

Android中layout_weight的属性理解的更多相关文章

  1. 【转】Android:Layout_weight的深刻理解

    原文网址:http://mobile.51cto.com/abased-375428.htm 本文详细介绍了Android布局中Layout_weight的属性,它是用来分配属于空间的一个属性,你可以 ...

  2. Android中的windowSoftInputMode属性详解

    这篇文章主要介绍了Android中的windowSoftInputMode属性详解,本文对windowSoftInputMode的9个属性做了详细总结,需要的朋友可以参考下     在前面的一篇文章中 ...

  3. Android:LinearLayout布局中Layout_weight的深刻理解

    首先看一下LinearLayout布局中Layout_weight属性的作用:它是用来分配属于空间的一个属性,你可以设置他的权重.很多人不知道剩余空间是个什么概念,下面我先来说说剩余空间. 看下面代码 ...

  4. android中布局文件中 layout_weight 的属性详解

    在不同的情况下,layout_weight属性作用是不同的.主要有两种属性: 1.当布局中的控件的尺寸(宽和高)都有指定时,它所表示的该控件在父容器中的比重,及它在父容器中所占的比例,数值越大,比重越 ...

  5. Android中Edittext的属性

    //此为转载别人的,挺不错的 1.EditText输入的文字为密码形式的设置 (1)通过.xml里设置: 把该EditText设为:android:password="true"  ...

  6. Android:Layout_weight的深刻理解

    最近写Demo,突然发现了Layout_weight这个属性,发现网上有很多关于这个属性的有意思的讨论,可是找了好多资料都没有找到一个能够说的清楚的,于是自己结合网上资料研究了一下,终于迎刃而解,写出 ...

  7. [转]Android:Layout_weight的深刻理解

    http://mobile.51cto.com/abased-375428.htm 最近写Demo,突然发现了Layout_weight这个属性,发现网上有很多关于这个属性的有意思的讨论,可是找了好多 ...

  8. android中xml tools属性详解

    第一部分 安卓开发中,在写布局代码的时候,ide可以看到布局的预览效果. 但是有些效果则必须在运行之后才能看见,比如这种情况:TextView在xml中没有设置任何字符,而是在activity中设置了 ...

  9. android中xmlns:tools属性详解

    今天读到一篇总结的非常棒的文章,写的逻辑很清晰也很实用,很少见到如此棒的文章了.就原文转发过来,我把格式给整理了一下,分享给园子里的各位朋友!好久没写博客了,就为2015年的11月留份纪念吧.希望对你 ...

随机推荐

  1. 挂载nfs系统问题之: Root-NFS: Server returned error -13 while mounting

    今天换了个路由器,由于是自动分的IP,现在的IP和之前的不在同一网段.以前是192.168.0.xxx,现在是192.168.1.xxx.本以为将serverip,ipaddr,bootargs这些参 ...

  2. bzoj4097 [Usaco2013 dec]Vacation Planning

    Description Air Bovinia is planning to connect the N farms (1 <= N <= 200) that the cows live ...

  3. awk--动作(action)

    摘要 在awk--简述中我们讲到awk是由pattern-action组合而成的,关于pattern我们已经awk--模式(pattern)在讲述,接下来就来看下awk的action. 动作是什么 我 ...

  4. Java中对象的上转型对象

    1. 定义 如果B类是A类的子类或间接子类,当用B类创建对象b并将这个对象b的引用赋给A类对象a时,如: A a;a = new B();ORA a;B b = new B();a = b; 则称A类 ...

  5. vector的含义

    数学中,vector(向量)表示一个量,由大小和方向构成.比如坐标中的一个带箭头的线段 -- 它1厘米长,正弦值六分之一π(30度角). 计算机的c++.java中,vector表示一种一维的数组.比 ...

  6. iPhone手机GPS地图位置好帮手

    十一国庆黄金周近在眉睫,我先祝大家过一个愉快开心的国庆长假. 假期内,难免老友聚会吃饭聊天联络感情,年轻朋友相亲约会,一家人出门旅游.平时,我们聚会时,总有要来的人找不到聚会地点,需要反复打电话确认: ...

  7. How to configure Gzip for JBoss?---refer

    Question: I think to try to speed up my Web App by reducing the size of transferred data. For exampl ...

  8. Android的图片压缩类ThumbnailUtils

    从Android 2.2开始系统新增了一个缩略图ThumbnailUtils类,位于framework包下的android.media.ThumbnailUtils位置,可以帮助我们从mediapro ...

  9. web页面打印

    在使用的两种方式打印: 第一种:js如下 function doPrint() { allhtml = window.document.body.innerHTML; starstr = " ...

  10. MySQL安装与测试

    工作室老师要求我们把MySQL装出来 今天折腾了下,本来不难的,不知道为什么用最新5.6.24的msi安装包,安装的时候选完路径后就没有后续了..蛋疼的我试了好几次,用cmd命令测试一直是 2003- ...