您的位置:首页 > 其它

ViewGroup.getChildMeasureSpec(int spec, int padding, int childDimension)解释

2016-09-21 14:22 399 查看
源码中的方法解释

/**
* Does the hard part of measureChildren: figuring out the MeasureSpec to
* pass to a particular child. This method figures out the right MeasureSpec
* for one dimension (height or width) of one child view.
*
* The goal is to combine information from our MeasureSpec with the
* LayoutParams of the child to get the best possible results. For example,
* if the this view knows its size (because its MeasureSpec has a mode of
* EXACTLY), and the child has indicated in its LayoutParams that it wants
* to be the same size as the parent, the parent should ask the child to
* layout given an exact size.
*
* @param spec The requirements for this view
* @param padding The padding of this view for the current dimension and
*        margins, if applicable
* @param childDimension How big the child wants to be in the current
*        dimension
* @return a MeasureSpec integer for the child
*/
public static int getChildMeasureSpec(int spec, int padding, int childDimension)


中文翻译:

计算子布局尺寸的最主要部分:为特定子控件计算出它的MeasureSpec,这个方法为一个子布局的任一一个维度(高度/宽度)计算出正确的MeasureSpec。

目标是结合父布局的MeasureSpec和子布局的LayoutParams去计算最合适的大小,例如,一个父布局知道自己的大小(因为它的MeasureSpec可以设置成EXACTLY),然后它的子控件指定和它一样大小,那这个时候父布局放置子控件的时候需要知道子控件的具体大小。

spec–viewGroup的属性,measureChildren传递过来的参数

padding–viewGroup的padding或者margin

childDimension–childView想要的大小

2. *方法实现解释 *

public static int getChildMeasureSpec(int spec, int padding, int childDimension) {

//获取viewGrou的Mode,size
int specMode = MeasureSpec.getMode(spec);
int specSize = MeasureSpec.getSize(spec);

//viewGroup内可以的最大尺寸
int size = Math.max(0, specSize - padding);

int resultSize = 0;
int resultMode = 0;

switch (specMode) {
// Parent has imposed an exact size on us
//ViewGroup是具体的值的情况
case MeasureSpec.EXACTLY:

//子view也是具体的值,则使用它具体的值
if (childDimension >= 0) {
resultSize = childDimension;
resultMode = MeasureSpec.EXACTLY;
} else if (childDimension == LayoutParams.MATCH_PARENT) {
// Child wants to be our size. So be it.
//想要match_parent,则使用ViewGroup最大的值size,mode是exactly
resultSize = size;
resultMode = MeasureSpec.EXACTLY;
} else if (childDimension == LayoutParams.WRAP_CONTENT) {
// Child wants to determine its own size. It can't be
// bigger than us.
//想要wrap_content,使用内容,用ViewGroup的最大值给他,
//mode是at_most
resultSize = size;
resultMode = MeasureSpec.AT_MOST;
}
break;

// Parent has imposed a maximum size on us
//ViewGroup本身是wrap_content
case MeasureSpec.AT_MOST:
if (childDimension >= 0) {
// Child wants a specific size... so be it
//子view可以是它想要的大小,mode是exactly
resultSize = childDimension;
resultMode = MeasureSpec.EXACTLY;
} else if (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;
} else if (childDimension == LayoutParams.WRAP_CONTENT) {
// Child wants to determine its own size. It can't be
// bigger than us.可以比父更大,mode是at_most
resultSize = size;
resultMode = MeasureSpec.AT_MOST;
}
break;

// Parent asked to see how big we want to be
//最矛盾的地方,一般都是父控件是AdapterView
case MeasureSpec.UNSPECIFIED:
if (childDimension >= 0) {
// Child wants a specific size... let him have it
resultSize = childDimension;
resultMode = MeasureSpec.EXACTLY;
} else if (childDimension == LayoutParams.MATCH_PARENT) {
// Child wants to be our size... find out how big it should
// be
resultSize = View.sUseZeroUnspecifiedMeasureSpec ? 0 : size;
resultMode = MeasureSpec.UNSPECIFIED;
} else if (childDimension == LayoutParams.WRAP_CONTENT) {
// Child wants to determine its own size.... find out how
// big it should be
resultSize = View.sUseZeroUnspecifiedMeasureSpec ? 0 : size;
resultMode = MeasureSpec.UNSPECIFIED;
}
break;
}
return MeasureSpec.makeMeasureSpec(resultSize, resultMode);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  源码