Android的layout_weight和weightSum
先看一下weightSum属性的功能描述:定义weight总和的最大值。如果未指定该值,以所有子视图的layout_weight属性的累加值作为总和的最大值。把weightSum的定义搁在这里,先去看看android:layout_weight如何使用。
android:layout_weight是用来给LinearLayout的子控件分配剩余空间,这里的剩余空间在某些情况下也会为负数。以计算子控件宽度为例(高度计算方式也一致),计算公式为:View宽度 = View原有宽度 +((LinearLayout宽度 - (所有子View宽度总和)) / 所有子View的weight总和) × View的weight值。按照我这里给出公式,计算下面的LinearLayout中的子View宽度:
我们假设屏幕宽度为480,假设TextView的wrap_content宽度为20
TextView1宽度为 = 20 +((480 - (20×3)) / 2) × 0 = 20
TextView2宽度为 = 20 +((480 - (20×3)) / 2) × 1 = 230
TextView3宽度为 = 20 +((480 - (20×3)) / 2) × 1 = 230
最后显示效果如下图所示:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" > <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#0045f5"
android:gravity="center"
android:text="1" /> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#00ff47"
android:gravity="center"
android:text="2"
android:layout_weight="1"/> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#ff5600"
android:gravity="center"
android:layout_weight="1"
android:text="3" /> </LinearLayout>
所以通过理解上面给出的公式,我们可以看出这个公式的第一步就是计算LinearLayout内的剩余空间,再将剩余空间按weight比例分配给各个子View。在公式中,每个子View的宽度等于定义时的View宽度(例如为wrap_content,则子View宽度等于自身内容宽度;fill_parent,则为填充父容器的宽度)+ 按weight分配的剩余空间宽度。如果我们将子View的宽度都设置成0dp,我们将完全可以按weight来分配子View的宽度。而反之,当我们将子View宽度设置为wrap_content时,控件宽度就会受到weight和子View本身定义时宽度两个因素影响。而当子View宽度设置为fill_parent时,对子View的影响更加明显,例如下面代码:
我们假设屏幕宽度为480,而TextView因为宽度设置为fill_parent,所以宽度也都是480
TextView1宽度为 = 480 +((480 - (480×3)) / 6) × 1 = 320
TextView2宽度为 = 480 +((480 - (480×3)) / 6) × 2 = 160
TextView3宽度为 = 480 +((480 - (480×3)) / 6) × 3 = 0
所以结果是TextView1占了2/3,TextView2占了1/3,TextView3没有显示空间。其实无论宽度如何设置,只要按给出公式计算,我们就能明白为什么最终显示效果如此。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" > <TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#0045f5"
android:gravity="center"
android:layout_weight="1"
android:text="1" /> <TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#00ff47"
android:gravity="center"
android:text="2"
android:layout_weight="2"/> <TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#ff5600"
android:gravity="center"
android:layout_weight="3"
android:text="3" />
</LinearLayout>
最后说一下weightSum,这个属性是设置在LinearLayout上,就像开头说的是设置weight总和的最大值。怎么理解呢?比如我们想让3个TextView按1:2:2的方式显示,需要如下代码所示设置。此时的weightSum其实是1+2+2=5,也就是说如果未指定LinearLayout的该值,以所有子视图的layout_weight属性的累加值作为总和的最大值。那如果我手动设置weightSum,则子View的weight必须按weightSum来分配。例如我们将weightSum设置为1,而TextView1的weight设置为0.2,TextView2的weight设置为0.4,TextView3的设置为0.4,同样能够实现1:2:2的效果。所以weightSum的作用就如其定义:“定义weight总和的最大值。如果未指定该值,以所有子视图的layout_weight属性的累加值作为总和的最大值。”
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" > <TextView
android:layout_width="wrap_content"
android:layout_height="0dp"
android:background="#0045f5"
android:gravity="center"
android:text="1"
android:layout_weight="1" /> <TextView
android:layout_width="wrap_content"
android:layout_height="0dp"
android:background="#00ff47"
android:gravity="center"
android:text="2"
android:layout_weight="2"/> <TextView
android:layout_width="wrap_content"
android:layout_height="0dp"
android:background="#ff5600"
android:gravity="center"
android:layout_weight="2"
android:text="3" /> </LinearLayout>
其实这里写的比较简单,主要是为了给自己做个笔记,把关键的稍微写一下。如果对大家帮助不大,还请见谅!
参考连接:
http://blog.csdn.net/xiaanming/article/details/13630837
http://book.51cto.com/art/201404/435840.htm
参考代码:http://files.cnblogs.com/files/endure/ListViewTable.rar(给出参考连接1中的Demo加工版)
Android的layout_weight和weightSum的更多相关文章
- Android布局中的layout_weight和weightSum属性的详解及使用
由于Android设备的尺寸大小不一,种类繁多,当我们在开发应用的时候就要考虑屏幕的适配型了,尽可能让我们的应用适用于主流机型的尺寸,这样我们的应用不会因为尺寸不同而不美观,解决屏幕适配问题的方法有很 ...
- Android之layout_weight解析
我们先来看以下这段Android布局代码: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/androi ...
- 【转】android中layout_weight的理解
android Layout_weight的理解 SDK中的解释: Indicates how much of the extra space in the LinearLayout will be ...
- 浅谈Android中layout_weight
引言 在开发android过程中,我们经常需要对界面进行按比例进行布局,我们一般都会使用layout_属性来进行设置.今天这篇文章我们就来简单介绍下layout_weight的使用和布局原理.随着做项 ...
- Android:Layout_weight的深刻理解
最近写Demo,突然发现了Layout_weight这个属性,发现网上有很多关于这个属性的有意思的讨论,可是找了好多资料都没有找到一个能够说的清楚的,于是自己结合网上资料研究了一下,终于迎刃而解,写出 ...
- android:layout_weight属性详解 (转)
在android开发中LinearLayout很常用,LinearLayout的内控件的android:layout_weight在某些场景显得非常重要,比如我们需要按比例显示.android并没用提 ...
- android:layout_weight属性详解(转)
在android开发中LinearLayout很常用,LinearLayout的内控件的android:layout_weight在某些场景显得非常重要,比如我们需要按比例显示.android并没用提 ...
- 【转】Android:Layout_weight的深刻理解
原文网址:http://mobile.51cto.com/abased-375428.htm 本文详细介绍了Android布局中Layout_weight的属性,它是用来分配属于空间的一个属性,你可以 ...
- Android中layout_weight的属性理解
https://www.zybuluo.com/zzudhj/note/102067 在Android开发过程中,在编写布局文件经常会用layout_weight属性:从字面意思上看权重.比值.按比例 ...
随机推荐
- Do in SDN
Do in SDN 书籍 <深度解析SDN 利益.战略.战术.实践> 张卫锋 <重构网络-SDN构架与实现>杨泽卫.李呈 <软件定义网络核心原理与应用实践> 黄韬. ...
- Team抢救最后一下
Team抢救最后一下 Task1:Team抢救的总结 秦玉: 非常荣幸能以团队的方式获得这次的小黄衫,这件衣服的意义不仅是这门课的奖励,也会是我们整个抢救团队的一个见证和回忆~ ...
- final-review
小组名称:飞天小女警 项目名称:礼物挑选小工具 小组成员:沈柏杉(组长).程媛媛.杨钰宁.谭力铭 会议时间:12月2号12点 会议内容: 设想和目标 1.我们的软件要解决什么问题?是否定义得很清楚?是 ...
- 课程回顾5in1
提出过的问题 问题1:敏捷开发在现阶段急于使用或试行,会不会得到相反的结果? 整个开发流程在施行了一整个学期,有积极的影响,也有消极的影响.例如通过这个流程的实施,规划短期的项目进度,使得成员能逐步了 ...
- EntityFramework中Json序列化的循环引用问题解决--Newtonsoft.Json
1.在使用EF时,由于数据库主外键关联,将对象进行Json序列化时会遇到循环引用的问题 //EF 中由于数据库主外键关联,对象的序列化经常出现循环引用问题 //使用.Net 自带的序列化工具,序列化出 ...
- 【设计模式】—— 装饰模式Decorator
前言:[模式总览]——————————by xingoo 模式意图 在不改变原来类的情况下,进行扩展. 动态的给对象增加一个业务功能,就功能来说,比生成子类更方便. 应用场景 1 在不生成子类的情况下 ...
- bug5 Debug:This kind of launch is configured to openthe debug perspective when it解决办法
启动tomcat时,myeclipse报错: This kind of launch is configured to openthe debug perspective when itsuspend ...
- Json对象和Json字符串的区别
说白了,字符串都是带引号的. 尤其是在使用springmvc的时候,后台@RequestBody接受的是一个json格式的字符串,一定是一个字符串. 参考这个博客还可以: https://blog. ...
- sqoop的安装与配置
最近需要将MySQL的数据导出到HDFS,所以搜到了sqoop2.跟sqoop1相比,sqoop2的好处是直接使用程序连接到集群上的sqoop,远程操作.流程是需要先创建link也可以理解成要操作的对 ...
- mongoDB的使用(NodeJs)
MongoDB相信大家都不太陌生了,但是究竟它和我们熟悉的MySQL(MariaSQL).SQLServer有什么区别呢? 我们熟悉的MySQL是关系型数据,每个数据表都有严格的数据结构规定,因而我们 ...