1. MeasureSpec.EXACTLY
The parent has determined an exact size for the child. The child is going to be given those bounds regardless of how big it wants to be.
2. MeasureSpec.AT_MOST
The child can be as large as it wants up to the specified size.
3. MeasureSpec.UNSPECIFIED
The parent has not imposed any constraint on the child. It can be whatever size it wants.
4. MeasureSpec创建规则
5. 调用关系
5.1 ViewGroup调用:onMeasure -> forEachChild(measureChildWithMargins -> getChildMeasureSpec -> View.measure -> View.onMeasure)
5.2 View调用:onMeasure -> setMeasuredDimension -> getDefaultSize -> getSuggestedMinimunWidth/getSuggestedMinimunHeight -> determinMeasureWidthorHeight
5.3 ViewGroup会重写View的onMeasure方法,父类View.onMeasure基本只是通过MeasureSpec确定大小而已,TextView等具体view会重写onMeasure方法。
5.4 ViewGroup会对包含的子View进行遍历测量,具体的子View的MeasureSpec数据由子View的onMeasure方法完成。
6. MeasureSpec影响因素
父容器的MeasureSpec和子View的LayoutParams共同决定了子View的MeasureSpec。在measure阶段View的宽和高由其measureSpec中的specSize决定
7. 扩展
clipToPadding:是否让子View裁剪以适应父View的padding,默认false
clipChildren:是否让子View裁剪当超过父View的边界,默认false
getLeft/getX:不要莫名其妙的将两个不同应用场景存在的方法进行对比区分,根据场景不就区分了吗。getLeft等于相对父View的距离,getX等于相对父View的左边距left+移动TranslationX。