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. bzoj1503 [NOI2004]郁闷的出纳员(名次树+懒惰标记)

    1503: [NOI2004]郁闷的出纳员 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 8705  Solved: 3027[Submit][Statu ...

  2. dojox.grid.EnhancedGrid 和 dojox.grid.DataGrid 的继承关系

    dojox.grid.EnhancedGrid  的介绍说, EnhancedGrid 是基于 DataGrid 提供增强功能的. EnhancedGrid (dojox.grid.EnhancedG ...

  3. HDU2050(分平面问题)

    分平面问题: 一.n条直线最多分平面问题. n条直线最多可以把平面分成多少个区域? 此类问题主要采用递归的思想.当有n-1条直线时,平面最多被分成了f(n-1)块区域.如果要使第n条直线分的区域最多, ...

  4. 修改上一篇文章的node.js代码,支持默认页及支持中文

    服务端 app.js var app = require('http').createServer(handler) var io = require('socket.io')(app); var f ...

  5. Java多线程编程(二)

    在 Java多线程编程(一) 中的多线程并没有返回值,本文将介绍带返回值的多线程. 要想有返回值,则需要实现新的接口Callable而不再是Runnable接口,实现的方法也改为call()方法,执行 ...

  6. [Redux] Writing a Todo List Reducer (Adding a Todo)

    Learn how to implement adding a todo in a todo list application reducer. let todo = (state = [], act ...

  7. Cocos2d-x 3.x 头像选择,本地相册图片+图片编辑(Android、IOS双平台)

    大连游戏产业不是很发达,最后,选择一个应用程序外包公司.积累的工作和学习过程中的一点业余生活微信体验,我想分享的游戏小朋友的爱. 在应用开发过程中会经常实用户上传头像的功能,在网上找了N多资料发现没有 ...

  8. Two-Phase-Commit for Distributed In-Memory Caches--reference

    Part I reference from:http://gridgain.blogspot.kr/2014/09/two-phase-commit-for-distributed-in.html 2 ...

  9. 27个Jupyter Notebook使用技巧及快捷键(翻译版)

    Jupyter Notebook Jupyter Notebook 以前被称为IPython notebook.Jupyter Notebook是一款能集各种分析包括代码.图片.注释.公式及自己画的图 ...

  10. karma、jasmine做angularjs单元测试

    引用文:karma.jasmine做angularjs单元测试 karma和jasmine介绍 <1>技术介绍 karma karma是Testacular的新名字 karma是用来自动化 ...