背景:在自己封装的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