ViewGroup.getChildMeasureSpec(int spec, int padding, int childDimension)解释
2016-09-21 14:22
399 查看
源码中的方法解释
中文翻译:
计算子布局尺寸的最主要部分:为特定子控件计算出它的MeasureSpec,这个方法为一个子布局的任一一个维度(高度/宽度)计算出正确的MeasureSpec。
目标是结合父布局的MeasureSpec和子布局的LayoutParams去计算最合适的大小,例如,一个父布局知道自己的大小(因为它的MeasureSpec可以设置成EXACTLY),然后它的子控件指定和它一样大小,那这个时候父布局放置子控件的时候需要知道子控件的具体大小。
spec–viewGroup的属性,measureChildren传递过来的参数
padding–viewGroup的padding或者margin
childDimension–childView想要的大小
2. *方法实现解释 *
/** * 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); }
相关文章推荐
- ViewGroup getChildMeasureSpec
- 自定义View之onMeasure(int widthMeasureSpec, int heightMeasureSpec)方法解释
- setMeasuredDimension(widthMeasureSpec, heightMeasureSpec)方法导致在视图中自定义ViewGroup一下的控件显示不出来
- Android getChildmeasureSpec 方法的分析
- ViewGroup getChildDrawingOrder与焦点Focus放大遮挡
- android ViewGroup getChildDrawingOrder与 isChildrenDrawingOrderEnabled()
- View onMeasure (int widthMeasureSpec, int heightMeasureSpec)
- View margin 和padding, getWidth 和getMeasureWidth的区别
- 关于getChildMeasureSpec()
- 为什么要重写View的onMeasure(int widthMeasureSpec, int heightMeasureSpec)方法
- view的getChildMeasureSpace方法总结
- ava.lang.NullPointerException: Attempt to invoke virtual method 'int android.view.ViewGroup.getPaddi
- ViewGroup.measureChildWithMargins() 学习笔记
- 复写ViewGroup之OnLayout onMeasure
- 没有为类型 ViewGroup.LayoutParams 定义方法 setMargins(int, int, int)
- ViewGroup.layout(int l, int t, int r, int b)四个输入参数的含意
- 重复多轮调用getGroupView的解决方案
- ViewGroup的onMeasure和onLayout分析
- onMeasure(int widthMeasureSpec, int heightMeasureSpec)
- ViewGroup.layout(int l, int t, int r, int b)四个输入参数的含意