Android View 测量与 MeasureSpec 类

2017-03-14  本文已影响58人  learner222

前言

前段时间写一个项目,在布局中出现了 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.(减少对象分配)

测量模式有三种:

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。

上一篇 下一篇

猜你喜欢

热点阅读