老渔哥网站建设公司,宁波网上预约挂号平台,施工企业安全生产评价表,什么是网站建设公司本文章是基于Android源码6.0讲解Activity加载显示基本流程首先上一张图给大家一个直观的了解首先一个布局页面的加载是在Activity中的setContentView(R.layout.res)开始;我们就从Acitvity源码中的setContentView方法入手public void setContentView(LayoutRes int layoutResID)…本文章是基于Android源码6.0讲解Activity加载显示基本流程首先上一张图给大家一个直观的了解首先一个布局页面的加载是在Activity中的setContentView(R.layout.res)开始;我们就从Acitvity源码中的setContentView方法入手public void setContentView(LayoutRes int layoutResID) {getWindow().setContentView(layoutResID);initWindowDecorActionBar();}通过源码我们可以看到layoutResID又传给了getWindow()中的setContentView(layoutResID)方法mWindow new PhoneWindow(this);public Window getWindow() {return mWindow;}getWindow()返回的是PhoneWindow的对象我们来看PhoneWindow中setContentView(layoutResID)方法PhoneWindow是一个隐藏类在源码sources/andorid-23/com/android/internal/policy中PhoneWindow中setContentView方法Overridepublic void setContentView(int layoutResID) {if (mContentParent null) {installDecor();//Activity新创建时mContentParent 为空调用installDecor方法} else if (!hasFeature(FEATURE_CONTENT_TRANSITIONS)) {mContentParent.removeAllViews();}if (hasFeature(FEATURE_CONTENT_TRANSITIONS)) {final Scene newScene Scene.getSceneForLayout(mContentParent, layoutResID,getContext());transitionTo(newScene);} else {mLayoutInflater.inflate(layoutResID, mContentParent);}mContentParent.requestApplyInsets();final Callback cb getCallback();if (cb ! null !isDestroyed()) {cb.onContentChanged();}}我们来看PhoneWindow中installDecor()方法中的关键部分private void installDecor() {if (mDecor null) {mDecor generateDecor(); //1mDecor.setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS);mDecor.setIsRootNamespace(true);if (!mInvalidatePanelMenuPosted mInvalidatePanelMenuFeatures ! 0) {mDecor.postOnAnimation(mInvalidatePanelMenuRunnable);}}if (mContentParent null) {mContentParent generateLayout(mDecor); //2...}mLayoutInflater.inflate(layoutResID, mContentParent);//3}在PhoneWindow源码中声明了 private DecorView mDecor;DecorView 是PhoneWindow中的一个内部类继承了FrameLayout,是一个帧布局private final class DecorView extends FrameLayout implements RootViewSurfaceTaker {...}1 首先调用mDecor generateDecor();返回的是一个 DecorView的对象这就是所有页面的跟布局protected DecorView generateDecor() {return new DecorView(getContext(), -1);}2 其次调用mContentParent generateLayout(mDecor);protected ViewGroup generateLayout(DecorView decor) {...} else if ((features (1 FEATURE_ACTION_MODE_OVERLAY)) ! 0) {layoutResource R.layout.screen_simple_overlay_action_mode;} else {// Embedded, so no decoration is needed.layoutResource R.layout.screen_simple;// System.out.println(Simple!);}View in mLayoutInflater.inflate(layoutResource, null);decor.addView(in, new ViewGroup.LayoutParams(MATCH_PARENT, MATCH_PARENT));mContentRoot (ViewGroup) in;ViewGroup contentParent (ViewGroup)findViewById(ID_ANDROID_CONTENT);...return contentParent;}这里的layoutResource是源码中的布局我们来看一下R.layout.screen_simple在sdk/platforms/android-23/data/res/layout路径中android:layout_widthmatch_parentandroid:layout_heightmatch_parentandroid:fitsSystemWindowstrueandroid:orientationverticalandroid:inflatedIdid/action_mode_barandroid:layoutlayout/action_mode_barandroid:layout_widthmatch_parentandroid:layout_heightwrap_contentandroid:theme?attr/actionBarTheme /android:idandroid:id/contentandroid:layout_widthmatch_parentandroid:layout_heightmatch_parentandroid:foregroundInsidePaddingfalseandroid:foregroundGravityfill_horizontal|topandroid:foreground?android:attr/windowContentOverlay /R.layout.screen_simple就是一个简单的线性布局通过mLayoutInflater.inflate和decor.addView添加到DecorView中再通过ViewGroup contentParent (ViewGroup)findViewById(ID_ANDROID_CONTENT);把R.layout.screen_simple中id为id/content的Framelayout赋值给contentParent3 最后通过mLayoutInflater.inflate(layoutResID, mContentParent);把我们写的布局添加到mContentParent中。未完待续...