DecorView的测量过程
为何在Activity中获取DecorView的LayoutParams,返回为null??
View decorView = getWindow().getDecorView();
ViewGroup.LayoutParams layoutParams = decorView.getLayoutParams();
LogUtil.d("debug", " decorView.layoutParams = " + layoutParams);
创建DecorView对象
generateDecor()方法:
protected DecorView generateDecor() { return new DecorView(getContext(), -1); }
DecorView是Activity的顶级View,一般来说它内部包含标题栏和内容栏(layout.xml,即mContentParent)
接下来就是调用了generateLayout(mDecor)来初始化DecorView的结构。
这样一个PhoneWindow.DecorView对象就创建完成了。
DecorView添加到窗口 通过WindowManagerGlobal.addView方法
//实例化ViewRootImpl对象 root = new ViewRootImpl(view.getContext(), display); //顶层视图DecorView设置LayoutParams view.setLayoutParams(wparams);
对应于 ViewRootImpl 类,是连接 WindowManager 和 DecorView 的纽带。
ActivityThread 中当 activity 对象被创建好后,会将 DecorView 加入到 Window中同时完成 ViewRootImpl 的创建并建立和 DecorView 的联系。
root = new ViewRootImpl(view.getContext(), display);
root.setView(view, wparams, panelParentView);
不理解? 这两个有什么区别?
DecorView的MeasureSpec的创建过程
在ViewRootImpl中的mearsureHierarchy方法中有一段代码展示了DecorView的MeasureSpec的创建过程:
childWidthMeasureSpec = getRootMeasureSpec(baseSize, lp.width); childHeightMeasureSpec = getRootMeasureSpec(desiredWindowHeight, lp.height); performMeasure(childWidthMeasureSpec, childHeightMeasureSpec);
看一下gerRootMeasureSpec方法:
/** * Figures out the measure spec for the root view in a window based on it's * layout params. * * @param windowSize * The available width or height of the window * * @param rootDimension * The layout params for one dimension (width or height) of the * window. * * @return The measure spec to use to measure the root view. */ private static int getRootMeasureSpec(int windowSize, int rootDimension) { int measureSpec; switch (rootDimension) { case ViewGroup.LayoutParams.MATCH_PARENT: // Window can't resize. Force root view to be windowSize. measureSpec = MeasureSpec.makeMeasureSpec(windowSize, MeasureSpec.EXACTLY); break; case ViewGroup.LayoutParams.WRAP_CONTENT: // Window can resize. Set max size for root view. measureSpec = MeasureSpec.makeMeasureSpec(windowSize, MeasureSpec.AT_MOST); break; default: // Window wants to be an exact size. Force root view to be that size. measureSpec = MeasureSpec.makeMeasureSpec(rootDimension, MeasureSpec.EXACTLY); break; } return measureSpec; }
参考: Android开发艺术探索-View的工作原理
Android View系统解析(下)
Android View源码解读:浅谈DecorView与ViewRootImpl
Android View 测量流程(Measure)完全解析
DecorView对象的创建详解
View测量机制详解—从DecorView说起 - Loader's Blog - CSDN博客
ViewRoot、DecorView、MeasureSpec和View的工作原理