美文网首页
Android View 测量与 MeasureSpec 类

Android View 测量与 MeasureSpec 类

作者: learner222 | 来源:发表于2017-03-14 21:29 被阅读58次

前言

前段时间写一个项目,在布局中出现了 ScrollView 嵌套 ListView,导致 ListView 只能显示出第一个 item,在网上查了一下,发现其中一种解决方案代码量非常少,是通过自定义一个 ListView,覆写其中的 onMeasure() 方法。代码如下:

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,
    MeasureSpec.AT_MOST);
    super.onMeasure(widthMeasureSpec, expandSpec);
}

代码中最重要的地方就是将 View 的测量模式(mode)修改为 AT_MOST。

View 的测量

Android 中的显示的按钮、文本框等组件,都是需要系统绘制出来的,而在绘制之前,系统就需要知道每一个组件的大小,宽高各是多少像素。而这些操作都是在 onMeasure() 方法中进行的,onMeasure() 方法可以得到 xml 布局文件中配置的宽高,然后可以自定义一些操作,最后调用 setMeasuredDimension() 方法。

onMeasure(int widthMeasureSpec, int heightMeasureSpec) 方法有两个参数,这两个参数都是 int 型的。虽然说是 int 型的参数,但是每个参数都包含 2 个信息,例如 widthMeasureSpec 这个 int 值的高 2 位为测量的模式,低 30 位为测量的大小,也就是 View 的宽的大小。之所以使用 int 类型,而不是定义一个专门的类,Google 官方的解释如下:

MeasureSpecs are implemented as ints to reduce object allocation.(减少对象分配)

测量模式有三种:

  • EXACTLY 精确模式
    如果我们将 View 的 layout_width 属性或 layout_height 属性设置为 match_parent 或者具体的值,如 50 dp 等,这是,测量模式就会被指定为 EXACTLY。
  • AT_MOST 最大值模式
    当我们将 View 的 layout_width 属性或 layout_height 属性指定为 wrap_content,测量模式就会被指定为 AT_MOST,这时 View 大小会随着它的子 View 大小的变化而变化。
  • UNSPECIFIED 不指定模式
    一般系统不会指定该模式,只有在自定义 View 时才会使用。

MeasureSpec 类

MeasureSpec 类封装了一些对 MeasureSpecs(指 onMeasure() 方法参数,int 类型的那个值) 的一些操作,比如 getMode(int) 从 int 类型的值中取出测量模式(也就是高 2 位),getSize(int) 从 int 类型的值中取出长度值(也就是低 30 位),makeMeasureSpec(int size, int mode) 根据 size 和 mode 获得 MeasureSpecs(int 类型的值)。

demo

写了一个 demo 来理解一下 View 测量的流程。

先新建一个 MyTextView 继承自 TextView,覆写其中的 onMeasure() 方法。

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        setMeasuredDimension(
                measureWidth(widthMeasureSpec),
                measureHeight(heightMeasureSpec)
        );
    }

    private int measureHeight(int heightMeasureSpec) {
        int result = 0;
        int specMode = MeasureSpec.getMode(heightMeasureSpec);
        int specSize = MeasureSpec.getSize(heightMeasureSpec);

        if (specMode == MeasureSpec.EXACTLY) {
            result = specSize;
        } else {
            result = 200;
            if (specMode == MeasureSpec.AT_MOST) {
                result = Math.min(result, specSize);
            }
        }
        return result;
    }

    private int measureWidth(int widthMeasureSpec) {
        int result = 0;
        int specMode = MeasureSpec.getMode(widthMeasureSpec);
        int specSize = MeasureSpec.getSize(widthMeasureSpec);

        if (specMode == MeasureSpec.EXACTLY) {
            result = specSize;
        } else {
            result = 200;
            if (specMode == MeasureSpec.AT_MOST) {
                result = Math.min(result, specSize);
            }
        }
        return result;
    }

然后在布局中添加 3 个 MyTextView。

    <cn.zheteng123.onmeasuretest.MyTextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#FF0000"/>

    <cn.zheteng123.onmeasuretest.MyTextView
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:background="#00FF00"/>

    <cn.zheteng123.onmeasuretest.MyTextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#0000FF"
        android:text="12345678998765432112345699999999999999999999"/>

最终的效果是这样子的。

demo 效果

可以看到第三个 TextView 比较特别,我们设置了宽度和长度为 wrap_content,但是它的宽度与高度并没有适应内容的长度。这是因为我们在覆写 onMeasure() 方法时,其中有一段是这样的:

result = 200;
if (specMode == MeasureSpec.AT_MOST) {
    result = Math.min(result, specSize);
}

当设置布局宽度或高度为 wrap_content 时,测量模式就会是 AT_MOST,在代码中,我们将 TextView 的宽度和高度限制在了 200 以内,超过 200 仍然设置 200。

相关文章

  • Android View 测量与 MeasureSpec 类

    前言 前段时间写一个项目,在布局中出现了 ScrollView 嵌套 ListView,导致 ListView 只...

  • Android view架构

    view测量与绘制 view的测量MeasureSpec:定义:由SpecMode(int)于SpecSize(i...

  • View之测量MeasureSpec

    View中的MeasureSpec View在测量过程中依赖于测量规则 MeasureSpec。 DecorVie...

  • 自定义view

    1 MeasureSpec类 1定义 测量规格类,是测量view宽高的依据,决定了一个视图view的宽高,宽规格:...

  • View的工作原理-v2

    ● MeasureSpec这是View类得一个静态内部类,可以理解为测量规格,和子View的测量有关,子View的...

  • 《Android群英传》3.2View的测量

    View的测量 MeasureSpec类是系统给我们提供的一个设计短小精悍功能强大的类,通过它来帮我们测量View...

  • Android 理解MeasureSpec

    Android View 的测量过程中使用到了MeasureSpec,正如其字面意思所表达的那个-“测量规格”。V...

  • View的测量

    View通过MeasureSpac类来测量大小。MeasureSpec是一个32位的int值,其中高2位为测量的模...

  • MeasureSpec 类是如何设计的

    在 Android 中自定义 View 时,onMeasure 方法中使用 MeasureSpec 类表示 Vie...

  • 无标题文章

    MeasureSpec是android给view提供表示view尺寸的类,实质是一个32位int值。 Measur...

网友评论

      本文标题:Android View 测量与 MeasureSpec 类

      本文链接:https://www.haomeiwen.com/subject/slohnttx.html