【转】android中layout_weight的理解
android Layout_weight的理解
SDK中的解释:
Indicates how much of the extra space in the LinearLayout will be allocated to the view associated with these LayoutParams. Specify 0 if the view should not be stretched. Otherwise the extra pixels will be pro-rated among all views whose weight is greater than 0.
重点有两个
- layout_weight表示LinearLayout中额外空间的划分(可能扩大应用layout_weight前的大小也可能缩小)。
- 按比例(layout_weight大小的比例)
以下说的都以 android:orientation="horizontal" 为例
看了一下源码,虽说不太懂,但了解了下大概意思,按照自己的理解总结一下,直接写一下简化的代码吧(下面的代码是LinearLayout源文件中一部分的精简,变量名称含义可能不准确,为叙述方便暂作此解释):

//Either expand children with weight to take up available space or
// shrink them if they extend beyond our current bounds
int delta = widthSize - mTotalLength;
if (delta != 0 && totalWeight > 0.0f) {
float weightSum = mWeightSum > 0.0f ? mWeightSum : totalWeight;
for (int i = 0; i < count; ++i) {
final View child = getVirtualChildAt(i); if (child == null || child.getVisibility() == View.GONE) {
continue;
} final LinearLayout.LayoutParams lp =
(LinearLayout.LayoutParams) child.getLayoutParams(); float childExtra = lp.weight;
if (childExtra > 0) {
int share = (int) (childExtra * delta / weightSum);
weightSum -= childExtra;
delta -= share;
int childWidth = child.getMeasuredWidth() + share;
if (childWidth < 0) {
childWidth = 0;
}
}
}
}

变量含义
- widthSize: LinearLayout的宽度
- mTotalLength: 所有子View的宽度的和(还没用考虑layout_weight)
- totalWeight: 所有子View的layout_weight的和
- mWeihtSUm: LinearLayout的android:weightSum属性
过程分析:
首先计算出额外空间(可以为负)如果额外空间不为0并且有子View的layout_weight不为0的话按layout_weight分配额外空间:
int delta = widthSize - mTotalLength;
if (delta != 0 && totalWeight > 0.0f) {
...
}
如果LinearLayout设置了weightSum则覆盖子View的layout_weight的和:
float weightSum = mWeightSum > 0.0f ? mWeightSum : totalWeight;
然后遍历LinearLayout的子元素,如果不为null且Visibility不为GONE的话,取得它的LayoutParams,如果它的layout_weight大于0,根据weightSum与它的weight计算出分配给它的额外空间

if (childExtra > 0) {
int share = (int) (childExtra * delta / weightSum);
weightSum -= childExtra;
delta -= share;
int childWidth = child.getMeasuredWidth() + share;
if (childWidth < 0) {
childWidth = 0;
}
}

网上有解释说layout_weight表示重要程度,表示划分额外空间的优先级,通过代码可以知道这种观点是错误的.layout_weight 表示划分的比例,至于当View的layout_width为fill_parent时layout_weight比例相反的问题按我的理解可以作以下解 释:
比如说如下XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#00ff00"
android:weightSum="0"
android:orientation="horizontal" > <Button
android:id="@+id/imageViewLoginState"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="1" >
</Button> <Button
android:id="@+id/imageViewLoginState1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="2" >
</Button> <Button
android:id="@+id/imageViewLoginState2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="2"
android:text="3" >
</Button> </LinearLayout>

按一般理解,3个Button的比例应该为1:1:2,但实际情况是这样的:

按我的理解,系统是这样设置按钮的大小的,变量名按前面代码的意义:
假设Container即LinearLayout的宽度为PARENT_WIDTH
三个按钮的宽度都是FILL_PARENT,所以在应用layout_weight之前,三个按钮的宽度都为PARENT_WIDTH
所以额外空间:
delta = PARENT_WIDTH - 3 * PARENT_WIDTH = -2 * PARENT
因为LinearLayout没有设置android:weightSum(默认为0,设置为0就当没设置吧),所以 mWeightSum = 1 + 1 +2 =4
所以:
第一个按钮的宽度为
PARENT_WIDTH + share = PARENT_WIDTH + (layout_weight * delta / mWeightSum)
= PARENT_WIDTH + (1 * (-2 * PARENT_WIDTH) /4)
= 1 /2 *PARENT_WIDTH
然后更新weightSum与delta:
weightSum -= childExtra;(=3)
delta -= share;(=-3/2 * PARENT_WIDTH)
第二个按钮的宽度为:
PARENT_WIDTH + share = PARENT_WIDTH + (layout_weight * delta / mWeightSum)
= PARENT_WIDTH + (1 * (-3 / 2 * PARENT_WIDTH) /3)
= 1 /2 *PARENT_WIDTH
更新weightSum与delta:
weightSum -= childExtra;(=2)
delta -= share;(=-PARENT_WIDTH)
第三个按钮的宽度为:
PARENT_WIDTH + share = PARENT_WIDTH + (layout_weight * delta / mWeightSum)
= PARENT_WIDTH + (2 * (- PARENT_WIDTH) /2)
= 0
所以最终的而已就是前两个按钮平分LinearLayout,第三个按钮消失了.
大致过程是这样,但不全对,比如如果上例中LinearLayout的weightSum设置为2的话,前两个按钮的宽度为0,但当计算第三个按钮 的宽度时mWeightSum = 0,但layout_weight * delta / mWeightSum无法计算,不知道系统怎么处理的,在我的能力之外了,weightSum为2时的效果图:

weightSum为3时的效果图:

SDK中说明的是,layout_weight表示额外空间怎么划分,要注意额外2字,要有额外的空间才可以将按比例将其分配给设置了 layout_weight的子View,所以,如果LinearLayout设置为WRAP_CONTENT的话是没有额外的空间 的,layout_weight就没有用处,所只要layout_width不设置为WRAP_CONTENT就行,也可以设置为具体的值,如果值太小的 话,额外空间为负,可能压缩子控件,使其大小比XML文件中定义的小,例如:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:background="#00ff00"
android:orientation="horizontal" > <Button
android:id="@+id/button1"
android:layout_width="60dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="1" >
</Button> <Button
android:id="@+id/button2"
android:layout_width="60dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="2" >
</Button> <Button
android:id="@+id/button3"
android:layout_width="60dp"
android:layout_height="fill_parent"
android:layout_weight="2"
android:text="3" >
</Button> </LinearLayout>

额外空间:
delta = 100- 3 * 60 = -80
mWeightSum = 1 + 1 +2 =4
所以:
第一个按钮的宽度为:
60+ share = 60 + (layout_weight * delta / mWeightSum)
= 60 + (1 * (-80) /4) = 40
然后:
weightSum -= childExtra;(=3)
delta -= share;(=-60)
第二个按钮的宽度为:
60 + share = 60 + (layout_weight * delta / mWeightSum)
= 60 + (2 * (-60) /3)
= 40
然后:
weightSum -= childExtra;(=2)
delta -= share;(=-40)
第三个按钮的宽度为:
60 + share = 60 + (layout_weight * delta / mWeightSum)
= 60 + (2 * (-40) /2)
= 20
效果图:

以下代码也说明了layout_weight表示额外空间的分配:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:background="#00ff00"
android:orientation="horizontal" > <Button
android:id="@+id/button1"
android:layout_width="60dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="1" >
</Button> <Button
android:id="@+id/button2"
android:layout_width="40dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="2" >
</Button> </LinearLayout>

额外空间为100,所以Button1的宽度为60+100/2=110,Button2的宽度为40+100/2=90。
【转】android中layout_weight的理解的更多相关文章
- Android中一个经典理解误区的剖析
今天,在Q群中有网友(@广州-包晴天)发出了网上的一个相对经典的问题,问题具体见下图. 本来是无意写此文的,但群里多个网友热情不好推却,于是,撰此文予以分析. 从这个问题的陈述中,我们发现,提问者明显 ...
- Android中layout_weight的属性理解
https://www.zybuluo.com/zzudhj/note/102067 在Android开发过程中,在编写布局文件经常会用layout_weight属性:从字面意思上看权重.比值.按比例 ...
- 浅谈Android中layout_weight
引言 在开发android过程中,我们经常需要对界面进行按比例进行布局,我们一般都会使用layout_属性来进行设置.今天这篇文章我们就来简单介绍下layout_weight的使用和布局原理.随着做项 ...
- Android中AIDL的理解与使用(二)——跨应用绑定Service并通信
跨应用绑定Service并通信: 1.(StartServiceFromAnotherApp)AIDL文件中新增接口: void setData(String data); AppService文件中 ...
- Android中AIDL的理解与使用(一)——跨应用启动/绑定Service
AIDL(Android Interface Definition Language)--安卓接口定义语言 一.startService/stopService 1.同一个应用程序启动Service: ...
- (四)Android中Context的理解与使用
一.Context的作用 Context可用于访问全局资源. public class MainActivity extends Activity { private TextView tv; @Ov ...
- Android中Context的理解及使用(二)——Application的用途和生命周期
实现数据共享功能: 多个Activity里面,可以使用Application来实现数据的共享,因为对于同一个应用程序来说,Application是唯一的. 1.实现全局共享的数据App.java继承自 ...
- Android中Context的理解及使用(一)——Context的作用
Context的作用:用来访问全局信息的接口,通过Context进行资源的访问. 1.Context获取字符串资源: public class MainActivity extends AppComp ...
- Android:LinearLayout布局中Layout_weight的深刻理解
首先看一下LinearLayout布局中Layout_weight属性的作用:它是用来分配属于空间的一个属性,你可以设置他的权重.很多人不知道剩余空间是个什么概念,下面我先来说说剩余空间. 看下面代码 ...
随机推荐
- GOF23设计模式之工厂模式(factory)
一.工厂模式概述 实现了创建者和调用者的分离 (1)分类 ①简单工厂模式 虽然某种程度不符合设计原则,但实际使用最多. ②工厂方法模式 不修改已有类的前提下,通过增加新的工厂类实现扩展. ③抽象工厂模 ...
- AD芯片的基准参考电压问题
基准参考电压的精度一般非常高的! AD芯片 : AD9226的基准参考电压 误差一般是 千分之一! 我之前用万用表测量AD9226的参考电压大概是1.89V(这款AD的正确参考电压应该是2V),所 ...
- HTTP 与TCP/IP 、Socket区别(一)
网络由下往上分为: 物理层-- 数据链路层-- 网络层-- IP协议 传输层-- TCP协议 会话层-- 表示层和应用层-- HTTP协议 1.TCP/IP连接 手机能够使用联网功能是因为手机底层实现 ...
- Py修行路 python基础 (十三)匿名函数 与 内置函数
一.匿名函数 1.定义: 匿名函数顾名思义就是指:是指一类无需定义标识符(函数名)的函数或子程序. 2.语法格式:lambda 参数:表达式 lambda语句中,开头先写关键字lambda,冒号前是 ...
- IDA Pro 权威指南学习笔记(七) - 其他 IDA 显示窗口
默认情况下 IDA 不会打开这些窗口,可通过 View -> Open Subviews 命令打开 Strings 窗口(Strings) Strings 窗口中显示的是从二进制文件中提取出的一 ...
- ndnarry元素处理
元素计算函数 ceil(): 向上最接近的整数,参数是 number 或 array floor(): 向下最接近的整数,参数是 number 或 array rint(): 四舍五入,参数是 num ...
- ATL向控件添加私有属性-成员变量
https://msdn.microsoft.com/zh-cn/library/cc451389(v=vs.71).aspx ------------------------------------ ...
- webrtc系列文章
WEBRTC RTP/RTCP协议族 2017-02-22 20:15 阅读(144) 评论(0) WebRTC 基于GCC的拥塞控制(下) 2017-02-22 15:44 阅读(108) 评论(0 ...
- git用法小结(1)--建立远程仓库
最近一直在学习使用git来管理自己的程序,总是今天东学一点,明天西凑一点,到用的时候,总是有些茫然不知所措. 在博客园里看见一篇老好的文章,教我们做笔记啦,但是做完笔记还是要记得总结哦! 来吧,让我们 ...
- java socket发送xml报文
ServerRun.java import java.io.InputStream; import java.net.ServerSocket; import java.net.Socket; pub ...