//此方法逻辑已经总结在下面的表中//这个方法是根据父类传递的规格以及子类的layout参数来共同确定新规格//参数spec是父亲传入的规格,padding是viewGroup的padding(水平或竖直)和子view的margin(水平或竖直)(如果都有的话),childDimension其实就是子view的layout参数(layoutParam的width或者height参数)
public static int getChildMeasureSpec(int spec, int padding, int childDimension) {
//viewGroup传递的mode和sizeint specMode = MeasureSpec.getMode(spec);
int specSize = MeasureSpec.getSize(spec);
//size去掉自己padding和子view的margin占用的空间intsize = Math.max(0, specSize - padding);
int resultSize = 0;
int resultMode = 0;
switch (specMode) {
// Parent has imposed an exact size on uscase MeasureSpec.EXACTLY:
//viewgroup大小是确定的if (childDimension >= 0) {
//子view的大小也是确定的(是个确定的数值,比如100,而不是wrap_content或者match_parent),那么可能确定子view新的测量规格大小确定,就是自己本身布局参数设置的size
resultSize = childDimension;
resultMode = MeasureSpec.EXACTLY;
} elseif (childDimension == LayoutParams.MATCH_PARENT) {
// Child wants to be our size. So be it.//子view是填充父窗体,那么子view的新测量规格大小确定,就是父控件的size去掉padding,margin后的剩余大小
resultSize = size;
resultMode = MeasureSpec.EXACTLY;
} elseif (childDimension == LayoutParams.WRAP_CONTENT) {
// Child wants to determine its own size. It can't be// bigger than us
resultSize = size;
resultMode = MeasureSpec.AT_MOST;
}
break;
// Parent has imposed a maximum size on uscase MeasureSpec.AT_MOST:
if (childDimension >= 0) {
// Child wants a specific size... so be it
resultSize = childDimension;
resultMode = MeasureSpec.EXACTLY;
} elseif (childDimension == LayoutParams.MATCH_PARENT) {
// Child wants to be our size, but our size is not fixed.// Constrain child to not be bigger than us.
resultSize = size;
resultMode = MeasureSpec.AT_MOST;
} elseif (childDimension == LayoutParams.WRAP_CONTENT) {
// Child wants to determine its own size. It can't be// bigger than us.
resultSize = size;
resultMode = MeasureSpec.AT_MOST;
}
break;
// Parent asked to see how big we want to becase MeasureSpec.UNSPECIFIED:
if (childDimension >= 0) {
// Child wants a specific size... let him have it
resultSize = childDimension;
resultMode = MeasureSpec.EXACTLY;
} elseif (childDimension == LayoutParams.MATCH_PARENT) {
// Child wants to be our size... find out how big it should// be
resultSize = 0;
resultMode = MeasureSpec.UNSPECIFIED;
} elseif (childDimension == LayoutParams.WRAP_CONTENT) {
// Child wants to determine its own size.... find out how// big it should be
resultSize = 0;
resultMode = MeasureSpec.UNSPECIFIED;
}
break;
}
return MeasureSpec.makeMeasureSpec(resultSize, resultMode);
}