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. Tomcat内存溢出详解【转载】

    本文转载自 http://elf8848.iteye.com/blog/378805 Java内存溢出详解 一.常见的Java内存溢出有以下三种: 1. java.lang.OutOfMemoryEr ...

  2. 非常全面的Linux知识点总结

    $1 我的Linux需求 Linux博大精深.我只在此讨论一些我对线上Linux机器维护人员的基本需求,比如装机,加硬盘,配网络.只讨论CentOS 6,或者类似的RHEL,当然Ubuntu也可以此类 ...

  3. css文本超出2行就隐藏并显示省略号

    之前在网上看到过这样的代码,感觉有的时候还是挺有用的,故留个笔记. display:-webkit-box; //将对象作为弹性伸缩盒子模型显示. -webkit-box-orient:vertica ...

  4. css 改变scroll样式

    /*定义滚动条高宽及背景 高宽分别对应横竖滚动条的尺寸*/ ::-webkit-scrollbar { width: 16px; height: 16px; background-color: #F5 ...

  5. js怪招(摘录篇)

    利用a标签自动解析URL 很多时候我们有从一个URL中提取域名,查询关键字,变量参数值等的需要,而万万没想到可以让浏览器方便地帮我们完成这一任务而不用我们写正则去抓取.方法就在JS代码里先创建一个a标 ...

  6. quick-cocos2dx学习笔记

    20140603 下载quick,拉开拉链,跑setup.bat(注意,setup事就是在系统环境变量里加入QUICK_COCOS2DX_ROOT,假设不运行这个的话,启动player时会报找不到fr ...

  7. Android app内语言环境切换

    逻辑很简单: 1  app内所有activity继承自BaseActivity或BaseActivity派生出来的子类,BaseActivity中维护了一个静态的 app Activity访问栈,在创 ...

  8. iPhone 各版本屏幕分辨率

    参考:http://www.paintcodeapp.com/news/iphone-6-screens-demystified

  9. VB中的Dictionary对象

    VB中的Dictionary对象 Dictionary对象不是VBA或Visual Basic实时语言的具体存在的部分,它是存在于Microsoft Scripting Runtime Library ...

  10. IIS与ASP.NET 通信机制深度剖析

    IIS5.X缺点: ISAPI 动态连接库被加载到InetInfo.exe 进程中,它和工作进程之间是一种典型的跨进程通信方式,尽管采用命名管道,但是仍然会带来性能的瓶颈. 所有的 ASP.NET 应 ...