博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
android:layout_weight的真实含义
阅读量:4346 次
发布时间:2019-06-07

本文共 1854 字,大约阅读时间需要 6 分钟。

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

 

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

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

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

再看如下布局:

 

[html]
  1. <LinearLayout  
  2.     android:layout_width="match_parent"  
  3.     android:layout_height="wrap_content"  
  4.     android:orientation="horizontal" >  
  5.   
  6.     <TextView  
  7.         android:layout_width="wrap_content"  
  8.         android:layout_height="wrap_content"  
  9.         android:layout_weight="1"  
  10.         android:background="@android:color/black"  
  11.         android:text="111"  
  12.         android:textSize="20sp" />  
  13.   
  14.     <TextView  
  15.         android:layout_width="wrap_content"  
  16.         android:layout_height="wrap_content"  
  17.         android:layout_weight="2"  
  18.         android:background="@android:color/holo_green_light"  
  19.         android:text="222"  
  20.         android:textSize="20sp" />  
  21. </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/seven1979/p/4201736.html

你可能感兴趣的文章
mate标签的用法总结
查看>>
struts2 validation.xml 注意点
查看>>
过滤器与监听器
查看>>
循环链表
查看>>
《批判性思维教程》
查看>>
POJ 1273 Drainage Ditches 最大流-Dinic
查看>>
hdu 2120 Ice_cream's world I
查看>>
摧残孩子的最佳方式,就是以慈爱之名请他吃冰淇淋!
查看>>
java使用siger 获取服务器硬件信息(CPU 内存 网络 io等)
查看>>
(2) LVS负载均衡:VS_TUN和VS_DR的arp问题
查看>>
test
查看>>
Centos7基本设置
查看>>
Xcode5 之后版本 修改项目名称
查看>>
JS 将 string 转换成为 number
查看>>
Asp.net 在 Postback 之后 执行 javascript 方法
查看>>
文件夹及文件操作
查看>>
Python后台开发Django(会话控制)
查看>>
【BZOJ-1260】涂色paint 区间DP
查看>>
Redis学习记录之Java中的初步使用
查看>>
Eclipse Regular expression grammar
查看>>