android:layout_weight的真实含义(转)
首先声明只有在Linearlayout中,该属性才有效。之所以Android:layout_weight会引起争议,是因为在设置该属性的同时,设置android:layout_width为wrap_content和match_parent会造成两种截然相反的效果。如下所示:
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:orientation="horizontal" >
- <TextView
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_weight="1"
- android:background="@android:color/black"
- android:text="111"
- android:textSize="20sp" />
- <TextView
- android: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的反而占了三分之二!
再看如下布局:
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:orientation="horizontal" >
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_weight="1"
- android:background="@android:color/black"
- android:text="111"
- android:textSize="20sp" />
- <TextView
- android: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就可以理解为占比了!
android:layout_weight的真实含义(转)的更多相关文章
- Android开发(二十七)——android:layout_weight的真实含义
android:layout_weight的真实含义是:一旦View设置了该属性(假设有效的情况下),那么该 View的宽度等于原有宽度(android:layout_width)加上剩余空间的占比! ...
- android:layout_weight的真实含义
首先声明只有在Linearlayout中,该属性才有效.之所以android:layout_weight会引起争议, 是因为在设置该属性的同时,设置android:layout_width为wrap_ ...
- android:layout_weight的真实含义/android:layout_gravity的条件
用layout_weight的时候,不要把宽度(或是高度,你想分配weight的那个)设成match_parent. android:layout_weight只适用于LinearLayout and ...
- android:layout_weight越大所占比例越大和越大所占比例越小的两个例子
摘要: 我的技术博客经常被流氓网站恶意爬取转载.请移步原文:http://www.cnblogs.com/hamhog/p/3907146.html,享受整齐的排版.有效的链接.正确的代码缩进.更好的 ...
- 【Android学习】android:layout_weight的用法实例
对于android:layout_weight的用法,用下面的例子来说明: <LinearLayout xmlns:android="http://schemas.android.co ...
- android layout_weight讲解
Layout_weight是线性布局,也就是LinearLayout里面用到的,下面通过实验来看这个Layout_weight的特性. 1.当控件的属性android:layout_width=&qu ...
- Android layout_weight的用法
android:layout_weight是指LinearLayout先给里面的控件分配完大小之后剩余空间的权重. 下面通过举例说明: <LinearLayout xmlns:android=& ...
- Android的LinearLayout中的权重android:layout_weight
当前EditText和Button部件只是适应了他们各自内容的大小,如下图所示: 这样设置对按钮来说很合适,但是对于文本框来说就不太好了,因为用户可能输入更长的文本内容.因此如果能够占满整个屏幕宽度会 ...
- android layout_weight 使用总结
今天在使用androidlayout_weight的时候遇到点奇怪的问题,就上网查了一下,发现这篇文章很详细,就转了过来,谢谢分享者,写的很详细. 在 android开发中LinearLayout很 ...
随机推荐
- 此实现不是 Windows 平台 FIPS 验证的加密算法的一部分
在实用VS编程的时候大家经常会遇到一个问题: 下面小编来为大家提供一个简单的解决方案: 1.在Windows中打开功能里输入regedit,回车打开注册表编辑器: 2.转到路径HKEY_LOCAL_M ...
- csuoj 1392: Number Trick
http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1392 1392: Number Trick Time Limit: 1 Sec Memory L ...
- Hdu 1214 圆桌会议
圆桌会议 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submis ...
- webstorm修改文件,webpack-dev-server不会自动编译刷新
重装了 webstorm ,从10升级到了2016 一升不要紧,打开老项目,开启webpakc-dev-server,然后改代码,发现浏览器不会自动刷新了!!! 这可急死我了,各种卸载webpack. ...
- asp.net MVC3 无法打开项目文件“E:\我们的项目\Project\HeatingMIS.Web\HeatingMIS.Web.csproj”。此安装不支持该项目类型。
在vs中打开mvc3项目,虽然装了mvc3,但是还是会遇到莫名其妙的错误,这是我在做开发的时候遇到的一个问题,附带解决方案,和大家分享一下 问题描述:无法打开项目文件“E:\我们的项目\Project ...
- django中文配置的问题
language_code:'zh-hans' 生成的语言文件请使用zh_Hans,这样才能你的app和系统的app完全翻译成中文,掉此坑的请在下面留言
- 如何运用TurboDemo创建视频示例
TurboDemo不仅可以速抓取屏幕截图,而且可高效制作出时尚美观的介绍.演示动画.软件模拟以及使用说明.下面的例子将会告诉使用者们如何分步创建一个视频示例,帮助使用者们快速的上手: 1.开启Turb ...
- zabbix_sender自定义监控
这里推荐这个博客 http://www.ttlsa.com/monitor/zabbix/ 在这里学一遍都不用买书,直接就能玩转zabbix了 回归正题 zabbix相关包下载地址:http://ww ...
- Centos7下Etcd集群搭建
一.简介 "A highly-available key value store for shared configuration and service discovery." ...
- git使用ssh密钥
1.查看本地是否有密钥对,如果存在就删除cd ~/.sshid_dsa id_dsa.pub 2.重新生成密钥对ssh-keygen -t rsa -C "your_email@yourem ...