主动调用measure测量ConstraintLayout不生效

背景:在自己封装的AlertDialog#onCreate中有一个自动包裹scrollView的操作,先去主动测量contentView的高度,如果超过设定的最大高度(screenHeight -offset)则将contentView包裹一个scrollView,再添加到setContent中。
在启动app时会弹出一个隐私确认弹窗,此弹窗的需求类似,在超过设定的最大高度后,包裹滚动。但此弹窗的最大高度小于AlertDialog默认最大高度,故在此业务逻辑上也需要先主动测量一个view的高度,然后决定是否包裹scrollView .

//主动测量当前view的高度,触发onMeasure
 private Rect getMeasuredRect(View view) {
   
        int widthMeasureSpec = MeasureSpec.makeMeasureSpec(mConfirmWidth,
                MeasureSpec.AT_MOST);
        int heightMeasureSpec = MeasureSpec.makeMeasureSpec(mMaxContentHeight,
                MeasureSpec.AT_MOST);
        view.measure(widthMeasureSpec, heightMeasureSpec);
        Rect mRect = new Rect();
        mRect.set(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
        return mRect;
    }

xml布局为ConstraintLayout嵌套几个TextView

问题:

发现在AlertDialog中主动测量过contentView的高度后,在业务逻辑中再此测量高度不生效,返回的是传入的confirmWidth&maxContentheight . 很奇怪。测试如果注释掉AlertDialog的测量代码,则业务中的测量正常. 跟了下ConstraintLayout源码发现问题。

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
   
        long time = System.currentTimeMillis();
        int REMEASURES_A = 0;
        int REMEASURES_B = 0;
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);
        int paddingLeft = this.getPaddingLeft();
        int paddingTop = this.getPaddingTop();
        this.mLayoutWidget.setX(paddingLeft);
        this.mLayoutWidget.setY(paddingTop);
        this.mLayoutWidget.setMaxWidth(this.mMaxWidth);
        this.mLayoutWidget.setMaxHeight(this.mMaxHeight);
        if (android.os.Build.VERSION.SDK_INT >= 17) {
   
            this.mLayoutWidget.setRtl(this.getLayoutDirection() == 1);
        }

        this.setSelfDimensionBehaviour(widthMeasureSpec, heightMeasureSpec);
        int startingWidth = this.mLayoutWidget.getWidth();
        int startingHeight = this.mLayoutWidget.getHeight();
        boolean runAnalyzer = false;
        if (this.mDirtyHierarchy) {
    // **此布尔值第二次测量为false ,不会执行updateHierarchy**
            this.mDirtyHierarchy = false;
            this.updateHierarchy();
            runAnalyzer = true;
        }
        // 后面代码省略。。。。
        }

    }
updateHierarchy会重新计算测量
private void updateHierarchy() {
   
        int count = this.getChildCount();
        boolean recompute = false;

        for(int i = 0; i < count; ++i) {
   
            View child = this.getChildAt(i);
            if (child.isLayoutRequested()) {
   
                recompute = true;
                break;
            }
        }

        if (recompute) {
   
            this.mVariableDimensionsWidgets.clear();
            this.setChildrenConstraints();
        }

    }

    private void setChildrenConstraints() {
   
        boolean isInEditMode = this.isInEditMode();
        int count = this.getChildCount();
        int helperCount;
        View child;
        if (isInEditMode) {
   
            for(helperCount = 0; helperCount < count; ++helperCount) {
   
                child = this.getChildAt(helperCount);

                try {
   
                    String IdAsString = this.getResources().getResourceName(child.getId());
                    this.setDesignInformation(0, IdAsString, child.getId());
                    int slashIndex
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值