DecorView creation and DecorView Creation
When the handleResumeActivity method of ActivityThread is called during Activity startup, A PhoneWindow object associated with Activity is obtained first, and DecorView is obtained through PhoneWindow.
PhoneWindow. java
Public final View getDecorView () {if (mDecor = null) {installDecor ();} return mDecor;} private void installDecor () {if (mDecor = null) {// generate DecorView mDecor = generateDecor (); mDecor. setDescendantFocusability (ViewGroup. FOCUS_AFTER_DESCENDANTS); mDecor. setIsRootNamespace (true);} if (mContentParent = null) {// determine the layout content in DecorView Based on the Window style. mContentParent is the first sub-View of DecorView, it is also the parent View mContentParent = generateLayout (mDecor); final DecorContentParent decorContentParent = (DecorContentParent) mDecor of setContentView () in Activity onCreate. findViewById (R. id. decor_content_parent );}...}
DecorView is an internal class of PhoneWindow, inheriting FrameLayout, and generateDecor () is also like our custom View is a new DecorView, but there is only one featureId parameter, if it is-1, indicates that this View is a DecorView.
protected DecorView generateDecor() { return new DecorView(getContext(), -1);}private final class DecorView extends FrameLayout { public DecorView(Context context, int featureId) { super(context); mFeatureId = featureId; }}
Therefore, the layout in DecorView is mainly determined by generateLayout (mDecor ).
Protected ViewGroup generateLayout (DecorView decor) {// obtain the Window style TypedArray a = getWindowStyle ();... final WindowManager windowService = (WindowManager) getContext (). getSysteService (Context. WINDOW_SERVICE); if (windowService! = Null) {final Display display = windowService. getdefadisplay display ();...} final Context context = getContext (); final int targetSdk = context.getApplicationInfo().tar getSdkVersion ();... windowManager. layoutParams params = getAttributes (); // Inflate the window decor int layoutResource; int features = getLocalFeatures (); if () {// determines whether features contain tags} else if () {// If features contains custom Title layoutResource = R. layout. screen_custom_title;} else {// if the window property does not have any decoration (no TitleBar, no ActionBar, etc.) layoutResource = R. layout. screen_simple;} mDecor. startChanging (); // You Can See That DecorView also loads the layout through inflate, the default layout resource of the system framework is in View in = mLayoutInflater under the frameworks \ base \ core \ res \ layout directory. inflate (layoutResource, null); decor. addView (in, new ViewGroup. layoutParams (MATCH_PARENT, MATCH_PARENT); mContentRoot = (ViewGroup) in; ViewGroup contentParent = (ViewGroup) findViewById (ID_ANDROID_CONTENT); if (contentParent = null) {// throw an exception }... mDecor. finishChanging (); return contentParent ;}
Therefore, DecorView is generated by loading the corresponding layout Resources in the frameworks \ base \ core \ res \ layout directory through the inflate () method based on different window attributes. As for how the LayoutInflate. inflate () method loads the layout file and parses and generates the View, it will be analyzed in the next article.