match_parent、wrap_parent、具体值 和 MeasureSpec 类中 mode 的对应关系

本文探讨了Android中match_parent、wrap_parent和具体值在测量规格(MeasureSpec)中的对应关系,指出在不考虑父view影响时,wrap_parent对应AT_MOST,match_parent对应EXACTLY,具体值也对应EXACTLY。然而,实际onMeasure方法中的测量模式可能因父view的自定义行为(如RelativeLayout)而变化,这可以通过查看ViewGroup的getChildMeasureSpec方法了解。附带了测试代码进行说明。

如果不考虑父 view 的影响,测试结果如下:

 * wrap_parent -> MeasureSpec.AT_MOST
 * match_parent -> MeasureSpec.EXACTLY
 * 具体值 -> MeasureSpec.EXACTLY

这是一个符合我们直觉的结果。

一个 view 的 onMeasure 方法最终得到的测量规格值(测量约束值)中包含的测量模式和上面不一定对的上,这是因为 onMeasure 方法中得到的测量规格值(测量约束值)是 measure 方法传过来的,父 view 在调用 measure 方法的时候可以根据自己的情况不使用 ViewGroup 提供的 measureChildren 方法,而改变上面的映射关系,比如 RelativeLayout 中就结合其他 Layout 属性综合确定 MeasureSpec 中的模式。上面的映射关系可以直接查看 ViewGroup 类的 getChildMeasureSpec 方法,这是最简单的 measure 子 view 的方式.

ViewGroup 中 measureChild 方法如下:

    protected void measureChild(View child, int parentWidthMeasureSpec,
            int parentHeightMeasureSpec) {
        final LayoutParams lp = child.getLayoutParams();

        final int childWidthMeasureSpec = getChildMeasureSpec(parentWidthMeasureSpec,
                mPaddingLeft + mPaddingRight, lp.width);
        final int childHeightMeasureSpec = getChildMeasureSpec(parentHeightMeasureSpec,
                mPaddingTop + mPaddingBottom, lp.height);

        child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
    }

getChildMeasureSpec 方法如下:

    public static int getChildMeasureSpec(int spec, int padding, int childDimension) {
        int specMode = MeasureSpec.getMode(spec);
        int specSize = MeasureSpec.getSize(spec);

        int size = Math.max(0, specSize - padding);

        int resultSize = 0;
        int resultMode = 0;

        switch (specMode) {
        // Parent has imposed an exa
val parent = sourceView.parent as? ViewGroup ?: return val index = parent.indexOfChild(sourceView) // 隐藏 sourceView,但不要再依赖它的宽高 sourceView.visibility = View.GONE val lp = sourceView.layoutParams newView.layoutParams = lp val parentWidth = parent.width val parentHeight = parent.height // 宽度规格 val widthSpec = when (lp.width) { ViewGroup.LayoutParams.MATCH_PARENT -> View.MeasureSpec.makeMeasureSpec(parentWidth, View.MeasureSpec.EXACTLY) ViewGroup.LayoutParams.WRAP_CONTENT -> View.MeasureSpec.makeMeasureSpec(parentWidth, View.MeasureSpec.AT_MOST) else -> View.MeasureSpec.makeMeasureSpec(lp.width, View.MeasureSpec.EXACTLY) } // 高度规格 val heightSpec = when (lp.height) { ViewGroup.LayoutParams.MATCH_PARENT -> View.MeasureSpec.makeMeasureSpec(parentHeight, View.MeasureSpec.EXACTLY) ViewGroup.LayoutParams.WRAP_CONTENT -> View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED) else -> View.MeasureSpec.makeMeasureSpec(lp.height, View.MeasureSpec.EXACTLY) } // 打印调试信息 fun modeToString(mode: Int): String = when (mode) { View.MeasureSpec.EXACTLY -> "EXACTLY" View.MeasureSpec.AT_MOST -> "AT_MOST" View.MeasureSpec.UNSPECIFIED -> "UNSPECIFIED" else -> "UNKNOWN" } Log.d("ViewReplacement", "lp.width=${lp.width}, lp.height=${lp.height}") Log.d( "ViewReplacement", "widthSpec: size=${View.MeasureSpec.getSize(widthSpec)}, mode=${modeToString(View.MeasureSpec.getMode(widthSpec))}" ) Log.d( "ViewReplacement", "heightSpec: size=${View.MeasureSpec.getSize(heightSpec)}, mode=${modeToString(View.MeasureSpec.getMode(heightSpec))}" ) // 执行测量布局 newView.measure(widthSpec, heightSpec) newView.layout( sourceView.left, sourceView.top, sourceView.left + newView.measuredWidth, sourceView.top + newView.measuredHeight ) // 插入到 sourceView 的位置 parent.addView(newView, index)解释这段代码
最新发布
09-19
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值