自定义ViewGroup过程中,需要在onMeasure里面对子View进行测量。这就需要我们了解具体的测量机制
测量子view的方式
1. 通过measureChild()、measureChildWithMargins()方法来完成
比如FrameLayout、LinerLayout、CoordinatorLayout、ViewPager2
2.调用ViewGroup的静态方法getChildMeasureSpec()
通过该方法直接获取目标子View的MeasureSpec,然后手动measure,比如ScrollView、NestedScrollView、DrawerLayout、TabLayout、ConstraintLayout
3.综上所述
measureChild和measureChildWithMargins里面也是会通过getChildMeasureSpec方法来获取MeasureSpec的,也就是说,的这些容器,在测量它们的子View之前,都是先通过getChildMeasureSpec方法来获取子View的宽高MeasureSpec,然后传给子View的measure方法的。
那么接下来看一下getChildMeasureSpec
的源码实现
public static int getChildMeasureSpec(int spec, int padding, int childDimension) {
int specMode = MeasureSpec.getMode(spec);
...