View 的测量

说明

一个 View 的大小,不单单是由他自己决定的,而是由他自己和他的父亲共同决定的。

MeasureSpec类

测量的类。一个32位值,高两位代表测量模式,低30位是测量值。

MeasureSpec api

public static int makeMeasureSpec(int size, int mode)
public static int getMode(int measureSpec)
public static int getSize(int measureSpec)

测量模式

测量模式图

UNSPECIFIED(0):

未指定,父母没有对孩子施加任何约束。它可以是它想要的任何尺寸。

  • 一般出现在 AdapterView 的 item 的 heightMode 中、ScrollView 的 childView 的heightMode 中;此种模式比较少见。
  • 注意 : 此模式下, 子孩子的 match_parent 属性不生效

EXACTLY(1):

父母已经确定了孩子的确切大小。不管孩子想长多大,他都会得到这些限制。

  • 一般当childView设置其宽、高为精确值dp/px、match_parent时,ViewGroup会将其设置为EXACTLY

AT_MOST(2):

在限制范围以内,孩子可以是它想要的大小

  • 一般当childView设置其宽、高为wrap_content时,ViewGroup会将其设置为AT_MOST

onMeasure

测量的方法。

View api:

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
protected final void setMeasuredDimension(int measuredWidth, int measuredHeight)

getDefaultSize 方法中测量的处理

public static int getDefaultSize(int size, int measureSpec) {
        int result = size;
        int specMode = MeasureSpec.getMode(measureSpec);
        int specSize = MeasureSpec.getSize(measureSpec);

        switch (specMode) {
        case MeasureSpec.UNSPECIFIED:
            result = size;
            break;
        case MeasureSpec.AT_MOST:
        case MeasureSpec.EXACTLY:
            result = specSize;
            break;
        }
        return result;
    }

这个文章还需要再更新了

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值