View 的 measure 方法

public final void measure (int widthMeasureSpec, int heightMeasureSpec)
Added in  API level 1

This is called to find out how big a view should be. The parent supplies constraint information in the width and height parameters.

The actual measurement work of a view is performed in onMeasure(int, int), called by this method. Therefore, only onMeasure(int, int) can and must be overridden by subclasses.

Parameters
widthMeasureSpec Horizontal space requirements as imposed by the parent
heightMeasureSpec Vertical space requirements as imposed by the parent

为了计算出 view 应该是多大就调用这个方法。该 view 的父 view 提供宽度和高度的约束信息。view 的实际的测量工作是在该方法调用的 onMeasure 中执行的。只有 onMeasure 能够而且必须被 view 的子类重载来达到更加高效和准确的测量过程。


参数:

widthMeasureSpec  父 view 强加的横向空间的需求,可以理解为宽度测量规格

heightMeasureSpec 父 view 强加的纵向空间的需求,可以理解为高度测量规格


测量规格就是父 view 添加的空间约束,因为一个 view 的大小可以设置为相对父 view 的大小和其他兄弟 view 的大小,比如 match_parent。所以在计算 view 的实际大小的时候需要考虑父 view 的约束。


总的来说,view 的 measure 方法的功能是提供给 view  系统在考虑父 view 的约束的情况下,计算 view 的实际大小。measure 方法不能被重载,因为 android 的 view 系统要使用该方法来实现 view 系统的功能。想要自定义一个 view 大小的计算过程,只能重载 onMeasure 方法。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值