深入理解MeasureSpec

一、MeasureSpec简介

MeasureSpec是Android View系统中用于描述View在MeasureSpec上的要求,定义为一个32位的int类型变量。

32位的MeasureSpec中的高2位表示MeasureSpec的模式(Mode),剩下的30位表示MeasureSpec的大小(Size)。

二、MeasureSpec的三种模式

MeasureSpec的三种模式分别是UNSPECIFIED、EXACTLY和AT_MOST。

1、UNSPECIFIED模式(即为0)表示此View对尺寸没有任何参考意义,父View可以按照子View实际尺寸分配给它任意的空间。对于UNSPECIFIED模式的子View,虽然可以设置任意的宽高,但是其实意义并不是很大,对大部分View没有意义。

2、EXACTLY模式(即为MeasureSpec.EXACTLY,值为1073741824)表示View的确切大小已知,此时MeasureSpec中的size表示View的大小。对于EXACTLY模式的子View,想要占据特定的大小,需要确切的设定尺寸,否则将会导致View的不确定位置。

3、AT_MOST模式(即为MeasureSpec.AT_MOST,值为-2147483648)表示此View的尺寸是最大可获得的尺寸。对于AT_MOST模式的View,应该尽量适应于制约因素,尽量使得其不大于Specified Size的尺寸,否则就是不确定的,会影响其他控件的布局。

三、MeasureSpec的计算规则

MeasureSpec是通过父View的MeasureSpec和子View的LayoutParams来计算得到的。MeasureSpec的计算规则包括以下步骤:

1、获取父View的MeasureSpec以及父View的padding值(因为padding的值也是父View尺寸的一部分,所以需要将padding的值算入到父View的尺寸中)。

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {    int widthMode = MeasureSpec.getMode(widthMeasureSpec);    int widthSize = MeasureSpec.getSize(widthMeasureSpec);    int heightMode = MeasureSpec.getMode(heightMeasureSpec);    int heightSize = MeasureSpec.getSize(heightMeasureSpec);    int paddingLeft = getPaddingLeft();    int paddingRight = getPaddingRight();    int paddingTop = getPaddingTop();    int paddingBottom = getPaddingBottom();    // 去除padding后的父View宽高    int parentWidth = widthSize - paddingLeft - paddingRight;    int parentHeight = heightSize - paddingTop - paddingBottom;}

2、通过MeasureSpec.makeMeasureSpec方法,将父View的MeasureSpec和LayoutParams中的尺寸相结合,来计算子View的MeasureSpec。

public static int makeMeasureSpec(int size, int mode) {    if (mode == UNSPECIFIED) {        return size;    } else {        return size + mode;    }}protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {    int widthMode = MeasureSpec.getMode(widthMeasureSpec);    int widthSize = MeasureSpec.getSize(widthMeasureSpec);    int heightMode = MeasureSpec.getMode(heightMeasureSpec);    int heightSize = MeasureSpec.getSize(heightMeasureSpec);    int paddingLeft = getPaddingLeft();    int paddingRight = getPaddingRight();    int paddingTop = getPaddingTop();    int paddingBottom = getPaddingBottom();    // 去除padding后的父View宽高    int parentWidth = widthSize - paddingLeft - paddingRight;    int parentHeight = heightSize - paddingTop - paddingBottom;    // 通过makeMeasureSpec方法,计算子View的宽度和高度MeasureSpec    int childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(100, MeasureSpec.EXACTLY);    int childHeightMeasureSpec = MeasureSpec.makeMeasureSpec(200, MeasureSpec.AT_MOST);}

3、通过measureChildWithMargins或measureChildren方法,测量出子View实际的宽度和高度。

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {    int widthMode = MeasureSpec.getMode(widthMeasureSpec);    int widthSize = MeasureSpec.getSize(widthMeasureSpec);    int heightMode = MeasureSpec.getMode(heightMeasureSpec);    int heightSize = MeasureSpec.getSize(heightMeasureSpec);    int paddingLeft = getPaddingLeft();    int paddingRight = getPaddingRight();    int paddingTop = getPaddingTop();    int paddingBottom = getPaddingBottom();    // 去除padding后的父View宽高    int parentWidth = widthSize - paddingLeft - paddingRight;    int parentHeight = heightSize - paddingTop - paddingBottom;    // 通过makeMeasureSpec方法,计算子View的宽度和高度MeasureSpec    int childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(100, MeasureSpec.EXACTLY);    int childHeightMeasureSpec = MeasureSpec.makeMeasureSpec(200, MeasureSpec.AT_MOST);    // 测量子View的宽高    measureChildren(childWidthMeasureSpec, childHeightMeasureSpec);}

四、MeasureSpec的常见使用场景

MeasureSpec的常见使用场景包括:TableView的单元格高度、ListView/RecyclerView的Item高度、自定义View的测量等。

1、TableView的单元格高度

public void setCellWidthAndHeight(int width, int height) {    LayoutParams lp = new LayoutParams(width, height);    if (mTableLayout != null) {        TableRow tableRow = mTableLayout.findViewById(ROW_ID + mRowIndex);        View childView = tableRow.findViewById(COLUMN_ID + mColumnIndex);        if (childView != null) {            int childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY);            int childHeightMeasureSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY);            childView.measure(childWidthMeasureSpec, childHeightMeasureSpec);        }    }}

2、ListView/RecyclerView的Item高度

public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {    View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item, null);    int parentWidth = parent.getMeasuredWidth();    int itemHeight = parentWidth / 2;    int childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(parentWidth, MeasureSpec.EXACTLY);    int childHeightMeasureSpec = MeasureSpec.makeMeasureSpec(itemHeight, MeasureSpec.EXACTLY);    itemView.measure(childWidthMeasureSpec, childHeightMeasureSpec);    return new MyViewHolder(itemView);}

3、自定义View的测量

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {    int widthMode = MeasureSpec.getMode(widthMeasureSpec);    int widthSize = MeasureSpec.getSize(widthMeasureSpec);    int heightMode = MeasureSpec.getMode(heightMeasureSpec);    int heightSize = MeasureSpec.getSize(heightMeasureSpec);    int paddingLeft = getPaddingLeft();    int paddingRight = getPaddingRight();    int paddingTop = getPaddingTop();    int paddingBottom = getPaddingBottom();    // 去除padding后的父View宽高    int parentWidth = widthSize - paddingLeft - paddingRight;    int parentHeight = heightSize - paddingTop - paddingBottom;    // 计算子View的宽高MeasureSpec    int childWidthMeasureSpec = getChildMeasureSpec(widthMeasureSpec, paddingLeft + paddingRight, LayoutParams.WRAP_CONTENT);    int childHeightMeasureSpec = getChildMeasureSpec(heightMeasureSpec, paddingTop + paddingBottom, LayoutParams.WRAP_CONTENT);    // 测量子View的宽高    measureChildren(childWidthMeasureSpec, childHeightMeasureSpec);}

原创文章,作者:小蓝,如若转载,请注明出处:https://www.506064.com/n/248930.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
小蓝小蓝
上一篇 2024-12-12 13:30
下一篇 2024-12-12 13:30

相关推荐

  • 深入解析Vue3 defineExpose

    Vue 3在开发过程中引入了新的API `defineExpose`。在以前的版本中,我们经常使用 `$attrs` 和` $listeners` 实现父组件与子组件之间的通信,但…

    编程 2025-04-25
  • 深入理解byte转int

    一、字节与比特 在讨论byte转int之前,我们需要了解字节和比特的概念。字节是计算机存储单位的一种,通常表示8个比特(bit),即1字节=8比特。比特是计算机中最小的数据单位,是…

    编程 2025-04-25
  • 深入理解Flutter StreamBuilder

    一、什么是Flutter StreamBuilder? Flutter StreamBuilder是Flutter框架中的一个内置小部件,它可以监测数据流(Stream)中数据的变…

    编程 2025-04-25
  • 深入探讨OpenCV版本

    OpenCV是一个用于计算机视觉应用程序的开源库。它是由英特尔公司创建的,现已由Willow Garage管理。OpenCV旨在提供一个易于使用的计算机视觉和机器学习基础架构,以实…

    编程 2025-04-25
  • 深入了解scala-maven-plugin

    一、简介 Scala-maven-plugin 是一个创造和管理 Scala 项目的maven插件,它可以自动生成基本项目结构、依赖配置、Scala文件等。使用它可以使我们专注于代…

    编程 2025-04-25
  • 深入了解LaTeX的脚注(latexfootnote)

    一、基本介绍 LaTeX作为一种排版软件,具有各种各样的功能,其中脚注(footnote)是一个十分重要的功能之一。在LaTeX中,脚注是用命令latexfootnote来实现的。…

    编程 2025-04-25
  • 深入剖析MapStruct未生成实现类问题

    一、MapStruct简介 MapStruct是一个Java bean映射器,它通过注解和代码生成来在Java bean之间转换成本类代码,实现类型安全,简单而不失灵活。 作为一个…

    编程 2025-04-25
  • 深入了解Python包

    一、包的概念 Python中一个程序就是一个模块,而一个模块可以引入另一个模块,这样就形成了包。包就是有多个模块组成的一个大模块,也可以看做是一个文件夹。包可以有效地组织代码和数据…

    编程 2025-04-25
  • 深入理解Python字符串r

    一、r字符串的基本概念 r字符串(raw字符串)是指在Python中,以字母r为前缀的字符串。r字符串中的反斜杠(\)不会被转义,而是被当作普通字符处理,这使得r字符串可以非常方便…

    编程 2025-04-25
  • 深入探讨冯诺依曼原理

    一、原理概述 冯诺依曼原理,又称“存储程序控制原理”,是指计算机的程序和数据都存储在同一个存储器中,并且通过一个统一的总线来传输数据。这个原理的提出,是计算机科学发展中的重大进展,…

    编程 2025-04-25

发表回复

登录后才能评论