在Activity
当中,咱们通常都会调用setContentView
方法来初始化布局。java
ContentView
相关的方法在Activity
当中,与ContentView
相关的函数有下面这几个,咱们先看一下它们的注释说明:android
/**
* Set the activity content from a layout resource. The resource will be
* inflated, adding all top-level views to the activity.
*
* @param layoutResID Resource ID to be inflated.
*
* @see #setContentView(android.view.View)
* @see #setContentView(android.view.View, android.view.ViewGroup.LayoutParams)
*/
public void setContentView(@LayoutRes int layoutResID) {
getWindow().setContentView(layoutResID);
initWindowDecorActionBar();
}
/**
* Set the activity content to an explicit view. This view is placed
* directly into the activity's view hierarchy. It can itself be a complex * view hierarchy. When calling this method, the layout parameters of the * specified view are ignored. Both the width and the height of the view are * set by default to {@link ViewGroup.LayoutParams#MATCH_PARENT}. To use * your own layout parameters, invoke * {@link #setContentView(android.view.View, android.view.ViewGroup.LayoutParams)} * instead. * * @param view The desired content to display. * * @see #setContentView(int) * @see #setContentView(android.view.View, android.view.ViewGroup.LayoutParams) */ public void setContentView(View view) { getWindow().setContentView(view); initWindowDecorActionBar(); } /** * Set the activity content to an explicit view. This view is placed * directly into the activity's view hierarchy. It can itself be a complex
* view hierarchy.
*
* @param view The desired content to display.
* @param params Layout parameters for the view.
*
* @see #setContentView(android.view.View)
* @see #setContentView(int)
*/
public void setContentView(View view, ViewGroup.LayoutParams params) {
getWindow().setContentView(view, params);
initWindowDecorActionBar();
}
/**
* Add an additional content view to the activity. Added after any existing
* ones in the activity -- existing views are NOT removed.
*
* @param view The desired content to display.
* @param params Layout parameters for the view.
*/
public void addContentView(View view, ViewGroup.LayoutParams params) {
getWindow().addContentView(view, params);
initWindowDecorActionBar();
}
复制代码
经过上面的注释,能够看到这4个方法的用途:bash
layouResId
对应的布局,并将它添加到activity
的顶级View
中。View
添加到activity
的布局当中,它的默认宽高都是ViewGroup.LayoutParams#MATCH_PARENT
。LayoutParams
。LayoutParams
,已经存在的View
不会被移除。这四种方法其实都是调用了PhoneWindow.java
中的方法,经过源码咱们能够发现setContentView(View view, ViewGroup.LayoutParams params)
和setContentView(@LayoutRes int layoutResID)
的步骤基本上是同样的,只不过是在添加到布局的时候,前者由于已经得到了View
的实例,所以用的是addView
的方法,然后者由于须要先inflate
,因此,使用的是LayoutInflater
。app
setContentView
方法下面咱们以setContentView(@LayoutRes int layoutResID)
为例,看一下具体的实现步骤:ide
setContentView
@Override
public void setContentView(int layoutResID) {
// Note: FEATURE_CONTENT_TRANSITIONS may be set in the process of installing the window
// decor, when theme attributes and the like are crystalized. Do not check the feature
// before this happens.
if (mContentParent == null) {
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();
}
mContentParentExplicitlySet = true;
}
复制代码
首先,咱们会判断mContentParent
是否为空,经过添加的代码咱们能够知道,这个mContentParent
其实就是layoutResId
最后渲染出的布局所对应的父容器,当这个ContentParent
为空时,调用了installDecor
,mContentParent
就是在里面初始化的。函数
installDecor()
private void installDecor() {
//若是DecorView不存在,那么先生成它,它实际上是一个FrameLayout。
if (mDecor == null) {
mDecor = generateDecor();
}
//若是`ContentParent`不存在,那么也生成它,此时传入了前面的`DecorView`
if (mContentParent == null) {
mContentParent = generateLayout(mDecor);
final DecorContentParent decorContentParent = (DecorContentParent) mDecor.findViewById(R.id.decor_content_parent);
if (decorContentParent != null) {
mDecorContentParent = decorContentParent;
}
}
}
复制代码
咱们能够看到,mDecor
是一个FrameLayout
,它和mContentParent
的关系是经过mContentParent = generateLayout(mDecor)
产生。布局
generateLayout(DecorView decor)
protected ViewGroup generateLayout(DecorView decor) {
//...首先根据不一样的状况,给`layoutResource`赋予不一样的值.
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);
if (contentParent == null) {
throw new RuntimeException("Window couldn't find content container view");
}
//...
return contentParent;
}
复制代码
在上面赋值的过程当中,咱们主要关注如下几个变量,mContentRoot/mContentParent/mDecorContent
:ui
mContentRoot
必定是mDecor
的下一级子容器。mContentParent
是mDecor
当中id
为R.id.content
的ViewGroup
,可是它和mDecor
的具体层级关系不肯定,这依赖于mContentRoot
是经过哪一个xml
渲染出来。mContentParent
必定是传入的layoutResId
进行 inflate
完成以后的父容器,它必定不会为空,不然会抛出异常,咱们setContentView(xxx)
方法传入的布局,就是它的子View
。// This is the top-level view of the window, containing the window decor.
private DecorView mDecor;
// This is the view in which the window contents are placed. It is either
// mDecor itself, or a child of mDecor where the contents go.
private ViewGroup mContentParent;
复制代码
mDecorContent
则是mDecor
当中id
为decor_content_parent
的ViewGroup
,可是也有可能mDecor
当中没有这个id
的View
,这须要依赖与咱们的mContentRoot
是使用了哪一个xml
来inflate
的。再回到前面setContentView
的地方,继续往下看,当mContentParent
不为空的时候,那么会移除它底下的全部子View
。 以后会调用mLayoutInflater.inflate(layoutResID, mContentParent);
方法,把传入的View
添加到mContentParent
当中,最后回调一个监听。this
final Callback cb = getCallback();
if (cb != null && !isDestroyed()) {
cb.onContentChanged();
}
复制代码
mContentParentExplicitlySet
标志位在setContentView
的最后,将mContentParentExplicitlySet
这个变量设置为true
,这个变量实际上是用在requestFeature
当中,也就是说,咱们必须在调用setContentView
以前,调用requestFeature
,不然就会抛出下面的异常:spa
@Override
public boolean requestFeature(int featureId) {
if (mContentParentExplicitlySet) {
throw new AndroidRuntimeException("requestFeature() must be called before adding content");
}
return super.requestFeature(featureId);
}
复制代码
所以:requestFeature(xxx)
必需要在调用setContentView(xxx)
以前。
addContentView(View view, ViewGroup.LayoutParams params)
下面咱们再来看一下,addContentView
方法:
@Override
public void addContentView(View view, ViewGroup.LayoutParams params) {
if (mContentParent == null) {
installDecor();
}
mContentParent.addView(view, params);
mContentParent.requestApplyInsets();
final Callback cb = getCallback();
if (cb != null && !isDestroyed()) {
cb.onContentChanged();
}
}
复制代码
能够看到,它和set
方法的区别就是,它在添加到mContentParent
以前,并无把mContentParent
的全部子View
都移除,而是将它直接添加进去,经过布局分析软件,能够看到mContentParent
的类型为ContentFrameLayout
,它实际上是一个FrameLayout
,所以,它会覆盖在mContentParent
已有子View
之上。
Activity
的Window
关联起来在上面的分析当中,咱们仅仅是初始化了一个DecorView
,并根据设置的Style
属性,传入的ContentView
来初始化它的子布局,可是这时候它还有真正和Activity
的Window
关联起来,关联的地方在ActivityThread.java
中:
final void handleResumeActivity(IBinder token,
boolean clearHide, boolean isForward, boolean reallyResume, int seq, String reason) {
r = performResumeActivity(token, clearHide, reason);
if (r != null) {
if (r.window == null && !a.mFinished && willBeVisible) {
r.window = r.activity.getWindow();
View decor = r.window.getDecorView();
decor.setVisibility(View.INVISIBLE);
ViewManager wm = a.getWindowManager();
WindowManager.LayoutParams l = r.window.getAttributes();
a.mDecor = decor;
l.type = WindowManager.LayoutParams.TYPE_BASE_APPLICATION;
l.softInputMode |= forwardBit;
if (r.mPreserveWindow) {
a.mWindowAdded = true;
r.mPreserveWindow = false;
// Normally the ViewRoot sets up callbacks with the Activity
// in addView->ViewRootImpl#setView. If we are instead reusing
// the decor view we have to notify the view root that the
// callbacks may have changed.
ViewRootImpl impl = decor.getViewRootImpl();
if (impl != null) {
impl.notifyChildRebuilt();
}
}
if (a.mVisibleFromClient && !a.mWindowAdded) {
a.mWindowAdded = true;
wm.addView(decor, l);
}
} else if (!willBeVisible) {
if (localLOGV) Slog.v(
TAG, "Launch " + r + " mStartedActivity set");
r.hideForNow = true;
}
} else {
}
}
复制代码
从源码中能够看到,若是在执行handleResumeActivity
时,以前DecorView
没有被添加到WindowManager
当中时,那么它的第一次添加是在onResume()
方法执行完以后添加的。