现在的位置: 首页 > 综合 > 正文

View 的 measure 方法

2018年01月10日 ⁄ 综合 ⁄ 共 942字 ⁄ 字号 评论关闭

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 方法。

抱歉!评论已关闭.