自定义view的measureSpec是谁的mode和size

本文介绍了自定义Android View时如何处理onMeasure过程,特别是widthMeasureSpec的来源和含义。widthMeasureSpec包含mode和size两部分,mode决定了View的尺寸约束,size则指示了尺寸值。它并非直接来源于XML,而是由父视图在测量过程中传递,根据父视图的尺寸和子视图的布局参数决定。在RelativeLayout的例子中,父视图的尺寸用于帮助子View确定其match_parent等情况下的尺寸。
摘要由CSDN通过智能技术生成

一、简单背景介绍

在自定义view(不是ViewGroup)时,需要重写父类的onMeausre(), onLayout(), onDraw():

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}

看了很多书和文章,都介绍了widthMeasureSpec是什么东西。他是一个MeasureSpec,32位的int,前两位记录了他的mode,后30位记录了他的size。

mode如下:

Mode Name Cool
00 UNSPECIFIED 0
01 EXACTLY 1073741824
10 AT_MOST -2147483648

但是widthMeasureSpec和heightMeasureSpec是从哪儿来的?他的mode和size是在xml中定义的mode和size?还是他父亲的mode和size?

二、探索onMeasure中的widthMeasureSpec

自定义view的xml文件如下:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.example.wangxuan.customviewheight.CustomView
        android:layout_width="300px"
        android:layout_height="400px"
        
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值