首先声明只有在Linearlayout中,该属性才有效。之所以android:layout_weight会引起争议,是因为在设置该属性的同时,设置android:layout_width为wrap_content和match_parent会造成两种截然相反的效果。如下所示:

[html] view plaincopyprint?
  1. <LinearLayout
  2. android:layout_width="match_parent"
  3. android:layout_height="wrap_content"
  4. android:orientation="horizontal" >
  5. <TextView
  6. android:layout_width="match_parent"
  7. android:layout_height="wrap_content"
  8. android:layout_weight="1"
  9. android:background="@android:color/black"
  10. android:text="111"
  11. android:textSize="20sp" />
  12. <TextView
  13. android:layout_width="match_parent"
  14. android:layout_height="wrap_content"
  15. android:layout_weight="2"
  16. android:background="@android:color/holo_green_light"
  17. android:text="222"
  18. android:textSize="20sp" />
 <LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal" ><TextViewandroid:layout_width="match_parent"android:layout_height="wrap_content"android:layout_weight="1"android:background="@android:color/black"android:text="111"android:textSize="20sp" /><TextViewandroid:layout_width="match_parent"android:layout_height="wrap_content"android:layout_weight="2"android:background="@android:color/holo_green_light"android:text="222"android:textSize="20sp" />

上面的布局将两个TextView的宽度均设为match_parent,一个权重为1,一个权重为2.得到效果如下:

可以看到权重为1的反而占了三分之二!

再看如下布局:

[html] view plaincopyprint?
  1. <LinearLayout
  2. android:layout_width="match_parent"
  3. android:layout_height="wrap_content"
  4. android:orientation="horizontal" >
  5. <TextView
  6. android:layout_width="wrap_content"
  7. android:layout_height="wrap_content"
  8. android:layout_weight="1"
  9. android:background="@android:color/black"
  10. android:text="111"
  11. android:textSize="20sp" />
  12. <TextView
  13. android:layout_width="wrap_content"
  14. android:layout_height="wrap_content"
  15. android:layout_weight="2"
  16. android:background="@android:color/holo_green_light"
  17. android:text="222"
  18. android:textSize="20sp" />
  19. </LinearLayout>
    <LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal" ><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_weight="1"android:background="@android:color/black"android:text="111"android:textSize="20sp" /><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_weight="2"android:background="@android:color/holo_green_light"android:text="222"android:textSize="20sp" /></LinearLayout>

即宽度为wrap_content,得到视图如下:

左边 TextView占比三分之一,又正常了。

android:layout_weight的真实含义是:一旦View设置了该属性(假设有效的情况下),那么该 View的宽度等于原有宽度(android:layout_width)加上剩余空间的占比!

设屏幕宽度为L,在两个view的宽度都为match_parent的情况下,原有宽度为L,两个的View的宽度都为L,那么剩余宽度为L-(L+L) = -L, 左边的View占比三分之一,所以总宽度是L+(-L)*1/3 = (2/3)L.事实上默认的View的weight这个值为0,一旦设置了这个值,那么所在view在绘制的时候执行onMeasure两次的原因就在这。

Google官方推荐,当使用weight属性时,将width设为0dip即可,效果跟设成wrap_content是一样的。这样weight就可以理解为占比了!

转载于:https://www.cnblogs.com/yanganok/p/4996704.html

android:layout_weight的真实含义相关推荐

  1. Android LinearLayout的android:layout_weight属性

    本文主要介绍Android LinearLayout的android:layout_weight属性意义 android:layout_weight为大小权重,相当于在页面上显示的百分比,它的计算是根 ...

  2. android:layout_weight属性的简单使用

    效果: style.xml <style name="etStyle2"><item name="android:layout_width"& ...

  3. 【Linux进程、线程、任务调度】一 Linux进程生命周期 僵尸进程的含义 停止状态与作业控制 内存泄漏的真实含义 task_struct以及task_struct之间的关系

    学习交流加(可免费帮忙下载CSDN资源): 个人微信: liu1126137994 学习交流资源分享qq群1(已满): 962535112 学习交流资源分享qq群2: 780902027 文章目录 1 ...

  4. 情况控件Android layout_weight用法图解

    时间紧张,先记一笔,后续优化与完善. 直接上代码和图片. 情况一: <?xml version="1.0" encoding="utf-8"?> & ...

  5. 60-网络分层的真实含义是什么?

    2.网络分层的真实含义是什么? 2.1.这四个问题你真的懂了吗? 因为教科书或者老师往往会打一个十分不恰当的比喻:为什么网络要分层呀?因为不同的层次之间有不同的沟通方式,这个叫作协议.例如,一家公司也 ...

  6. 网络分层的真实含义是什么

    网络分层的真实含义是什么? 来自极客时间 当你听到什么二层设备.三层设备.四层LB和七层LB中层的时候,是否有点一头雾水,不知道这些所谓的层,对应的各种协议具体要做什么"工作"? ...

  7. Android 7.0真实上手体验

    Android 7.0真实上手体验 Android 7.0的首个开发者预览版发布了,支持的设备只有Nexus6.Nexus 5X.Nexus 6P.Nexus 9.Nexus Player.Pixel ...

  8. android不同的日志含义,Android EventLog各种标签含义

    Android EventLog各种标签含义 1. Eventlog获取 1. adb shell logcat -b events 可以获取如下的event log 1. 02-13 11:31:4 ...

  9. 怎么弄Windows 7 Aero特效主题真实含义

    Windows 7桌面小组的一研发成员向媒体透露了Windows 7 Aero主题的真实含义.他说,包括Windows Vista在内,Aero在功能方由三部分组成,分别是Aero Shake.Aer ...

最新文章

  1. java robot类自动截屏
  2. OpenCV中函数imread的参数flags的枚举值(可设置值)及其具体意义
  3. 编程实现表1(L的输入-输出模式分布),证实表1的正确性
  4. 第九章 转移指令的原理
  5. Mac os下android studio模拟器无法联网解决方法
  6. [置顶] Android的IPC访问控制设计与实现
  7. 选择不相交区间(贪心:求不相交区间最多个数)
  8. 新建项目虚拟环境及pycharm配置
  9. AFNetworking框架下的SSL服务器证书的自定义验证
  10. python对XML 操作
  11. 凤凰号首次传回未加工的火星照片,人类加紧施展征服火星计划
  12. 半圆形进度条(vue加强版)
  13. 无法打开excel powermap 三维地图
  14. Maven的基本概念(三)
  15. 几个必须知道的Python工具
  16. wifi爆破学习记录
  17. 《树莓派实战秘籍》——1.15 技巧15连接GPIO脚到面包板上
  18. 高高兴兴看视频,认认真真写程序
  19. 区块链应用_供应链金融
  20. 我国软件昂首阔步向前走

热门文章

  1. 【转】三天学好ADO
  2. Android Prelink
  3. 我国数字出版发展尚存三大难题
  4. 字符串插入_动态规划----字符串编辑最小距离
  5. ​网页图表Highcharts实践教程之标签组与载入动画
  6. Linux的load导入语句,LOAD DATA INFILE语句导入数据进入MySQL的一些注意事项
  7. cordova 不安全 css,[译] CSS Flexbox 中安全/不安全的对齐方式
  8. java class 生成对象_Java反射机制(创建Class对象的三种方式)
  9. 最新大脑图谱研究表明,手部的运动区域也与整个身体相连
  10. psychopy: pyglet.media.sources.riff.WAVEFormatException: AVbin is required to decode compressed medi