前言
看源码是一个枯燥的过程,为了达到效果,我们需要带着问题,有目的地去看源码
问题
我们经常使用的Activity的
onCreate
方法里setContentView(R.layout.activity_main)
是如何加载我们的布局文件的?
源码分析
- 找到
Activity
类的setContentView
方法:
public void setContentView(@LayoutRes int layoutResID) {
getWindow().setContentView(layoutResID);
initWindowDecorActionBar();
}
第一行代码调用了getWindow()
方法来获取Window
类的对象,然后调用了Window
类的setContentView
方法
- 我们找到
Window
类:
/**
* Abstract base class for a top-level window look and behavior policy. An
* instance of this class should be used as the top-level view added to the
* window manager. It provides standard UI policies such as a background, title
* area, default key processing, etc.
*
* <p>The only existing implementation of this abstract class is
* android.view.PhoneWindow, which you should instantiate when needing a
* Window.
*/
public abstract class Window {
/** Flag for the "options panel" feature. This is enabled by default. */
public static final int FEATURE_OPTIONS_PANEL = 0;
...
...
...
public abstract void setContentView(@LayoutRes int layoutResID);
...
...
...
}
这里Window
是个抽象类,而我们从注释The only existing implementation of this abstract class is android.view.PhoneWindow
唯一的实现类是PhoneWindow
,也就是实际调用的是PhoneWindow
- 我们找到
PhoneWindow
类的setContentView
方法:
public void setContentView(int layoutResID) {
...
...
...
if (hasFeature(FEATURE_CONTENT_TRANSITIONS)) {
final Scene newScene = Scene.getSceneForLayout(mContentParent, layoutResID,
getContext());
transitionTo(newScene);
} else {
mLayoutInflater.inflate(layoutResID, mContentParent);
}
...
...
...
}
这里我们就只分析核心代码了,其他的先不看,这里可以看到如果没有转场动画,就调用mLayoutInflater.inflate(layoutResID, mContentParent)
,我们这里先把传入的mContentParent
看作是一个布局父控件
- 转到
LayoutInflater
的inflate(int resource,ViewGroup root)
方法:
public View inflate(@LayoutRes int resource, @Nullable ViewGroup root) {
return inflate(resource, root, root != null);
}
这里调用了inflate(resource, root, root != null)
public View inflate(@LayoutRes int resource, @Nullable ViewGroup root, boolean attachToRoot) {
final Resources res = getContext().getResources();
if (DEBUG) {
Log.d(TAG, "INFLATING from resource: \"" + res.getResourceName(resource) + "\" ("
+ Integer.toHexString(resource) + ")");
}
final XmlResourceParser parser = res.getLayout(resource);
try {
return inflate(parser, root, attachToRoot);
} finally {
parser.close();
}
}
看到XmlResourceParser
应该不会感到陌生了吧,接下来看调用inflate(parser, root, attachToRoot)
如何解析xml布局文件
public View inflate(XmlPullParser parser, @Nullable ViewGroup root, boolean attachToRoot) {
synchronized (mConstructorArgs) {
...
...
...
View result = root;
try {
// Look for the root node.
int type;
while ((type = parser.next()) != XmlPullParser.START_TAG &&
type != XmlPullParser.END_DOCUMENT) {
// Empty
}
if (type != XmlPullParser.START_TAG) {
throw new InflateException(parser.getPositionDescription()
+ ": No start tag found!");
}
final String name = parser.getName();
if (DEBUG) {
System.out.println("**************************");
System.out.println("Creating root view: "
+ name);
System.out.println("**************************");
}
if (TAG_MERGE.equals(name)) {
if (root == null || !attachToRoot) {
throw new InflateException("<merge /> can be used only with a valid "
+ "ViewGroup root and attachToRoot=true");
}
rInflate(parser, root, inflaterContext, attrs, false);
} else {
// Temp is the root view that was found in the xml
final View temp = createViewFromTag(root, name, inflaterContext, attrs);
ViewGroup.LayoutParams params = null;
if (root != null) {
if (DEBUG) {
System.out.println("Creating params from root: " +
root);
}
// Create layout params that match root, if supplied
params = root.generateLayoutParams(attrs);
if (!attachToRoot) {
// Set the layout params for temp if we are not
// attaching. (If we are, we use addView, below)
temp.setLayoutParams(params);
}
}
if (DEBUG) {
System.out.println("-----> start inflating children");
}
// Inflate all children under temp against its context.
rInflateChildren(parser, temp, attrs, true);
if (DEBUG) {
System.out.println("-----> done inflating children");
}
// We are supposed to attach all the views we found (int temp)
// to root. Do that now.
if (root != null && attachToRoot) {
root.addView(temp, params);
}
// Decide whether to return the root that was passed in or the
// top view found in xml.
if (root == null || !attachToRoot) {
result = temp;
}
}
} catch (XmlPullParserException e) {
InflateException ex = new InflateException(e.getMessage());
ex.initCause(e);
throw ex;
} catch (Exception e) {
InflateException ex = new InflateException(
parser.getPositionDescription()
+ ": " + e.getMessage());
ex.initCause(e);
throw ex;
} finally {
// Don't retain static reference on context.
mConstructorArgs[0] = lastContext;
mConstructorArgs[1] = null;
}
Trace.traceEnd(Trace.TRACE_TAG_VIEW);
return result;
}
}
这段代码xml解析,如果解析到merge
标签,就调用rInflate(parser, root, inflaterContext, attrs, false)
,如果不是则先createViewFromTag(root, name, inflaterContext, attrs)
创建一个view
,然后调用root.generateLayoutParams(attrs)
来生成LayoutParams
,然后递归调用rInflateChildren(parser, temp, attrs, true)
遍历所有的标签完成解析,最后root.addView(temp, params)
完成界面加载.
最后附上一张布局文件解析流程图