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. Hdu 1856(离散化+并查集)More is better

    题意:一些人遵循朋友的朋友也是朋友原则,现在找出最大的朋友圈, 因为人的编号比较大,但是输入的数据最多是10w行,所以可得出最多也就20w人,所以可以进行一下离散化处理,这样数据就会毫无压力 //// ...

  2. sublime 生成网页头文件

    1.普通HTML 输入html:xt然后按tab键即可生成如下: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional// ...

  3. jquery表单实时验证

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

  4. homebrew介绍

    对于一个习惯了在 Ubuntu 的终端上通过 apt-get 来安装工具软件的我来说,也希望在Mac上找到类似的工具,能很方便的一条命令就能安装所需的软件,而不用手工的去查找下载编译,或者是折腾安装所 ...

  5. HDU 2485 Destroying the bus stations (IDA*+ BFS)

    传送门:http://acm.hdu.edu.cn/showproblem.php?pid=2485 题意:给你n个点,m条相连的边,问你最少去掉几个点使从1到n最小路径>=k,其中不能去掉1, ...

  6. Robotium--scroll操作系列

    上下滚动 scrollDown public boolean scrollDown() Scrolls down the screen. Returns: true if more scrolling ...

  7. [AngularJS] Exploring the Angular 1.5 .component() method

    Angualr 1.4: .directive('counter', function counter() { return { scope: {}, restrict: 'EA', transclu ...

  8. [转] Linux下查看用户列表

    原文地址:http://xiaod.in/read.php?77 俺的centos vps上面不知道添加了多少个账户,今天想清理一下,但是以前还未查看过linux用户列表,google了一下,找到方便 ...

  9. oracle手动删除数据库

    有时候,无法使用图形界面时,我们需要手动删除数据库,具体操作步骤如下:一.手动删除文件系统数据库   1.停止监听,防止有新的连接产生,同时,在数据库配置了em的,也需要停止 $ lsnrctl st ...

  10. HDU -2524 矩形A + B

    找规律题,这种题目比较巧妙,要仔细观察找出规律 1. 假设只有一行,一共有n列,那么由一个小矩形构成的矩形个数为n, 由两个小矩形构成的矩形个数为 n - 1个 .... 由 n 个小矩形构成的矩形个 ...