LayoutInflate部分源码解析

LatoutInflate 主要的作用是实例化一个 xml 文件,使得开发者获取一个 View 的实例(也可以看着把一个 xml 文件渲染成一个视图)。在 LayoutInflate 的内部,LayoutInflate 拿到自身的实例,一般通过 android.app.Activity#getLayoutInflater() 方法,或者通过 Context#getSystemService 来拿到已经在应用中的 LayoutInflate 实例(系统早已经配置好了 LayoutInflate 实例)。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[源码 1]
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); // 1
} finally {
parser.close();
}
}

在 [源码 1] 中,inflate(@LayoutRes int resource, @Nullable ViewGroup root, boolean attachToRoot) 方法里面三个参数分别表示:

  • resource 参数表示需要渲染的 xml 布局资源
  • root 如果 attachToRoot 设置为 true,则生成结构层次的父级; 如果设置为 false,则返回结构层次提供的一组 LayoutParams 值的对象(这个值可能为空); [ViewGroup 是一个包含其他视图的特殊的 View,可以看作一个视图容器]
  • attachToRoot 渲染的视图的结构层次是否捆绑父视图的参数,如果设置为 false,则表示需要捆绑;

在 [源码 1] 中的解析 1处,会调用方法 inflate(parser, root, attachToRoot),下面是查看其源码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
[源码 2]
public View inflate(XmlPullParser parser, @Nullable ViewGroup root, boolean attachToRoot) {
synchronized (mConstructorArgs) {

...
final AttributeSet attrs = Xml.asAttributeSet(parser); //返回 xml 资源布局的属性集合
...
View result = root;

try {
// 查找视图的跟视图节点
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) { //根视图为空 || attachToRoot 为 false
throw new InflateException("<merge /> can be used only with a valid "
+ "ViewGroup root and attachToRoot=true");
}
rInflate(parser, root, inflaterContext, attrs, false);
} else {
// 把当前 Xml 布局实例化,并且拿到该 View 的实例
final View temp = createViewFromTag(root, name, inflaterContext, attrs);

ViewGroup.LayoutParams params = null;

if (root != null) { //当根节点不为空的时候,创建根视图的节点的参数
// Create layout params that match root, if supplied
params = root.generateLayoutParams(attrs); // 2
if (!attachToRoot) { //为 false 的时候根视图绑定参数
// Set the layout params for temp if we are not
// attaching. (If we are, we use addView, below)
temp.setLayoutParams(params); // 3
}
}
...
//再次渲染根视图下面的子视图
rInflateChildren(parser, temp, attrs, true);
...
// 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) {

} catch (Exception e) {

} finally {

}

return result;
}
}

上面 [源码 2] 主要的逻辑是把从 inflate(XmlPullParser parser, @Nullable ViewGroup root, boolean attachToRoot) {synchronized (mConstructorArgs) 的方法中传进的 root 参数(该参数为需要渲染的 xml 布局),经过 createViewFromTag(root, name, inflaterContext, attrs) 的方法拿到该 root 视图的实例,因为 root 不为空,所以在注释 2、3 处,为该视图布局绑定布局参数,带视图参数返回视图的实例;
如果 root 参数在 inflate 方法传进来的时候为空,侧直接返回该 xml 视图的实例,但没有绑定布局参数;

本文标题:LayoutInflate部分源码解析

文章作者:

发布时间:2018年04月03日 - 11:04

最后更新:2021年06月20日 - 19:06

原始链接:https://hndroid.github.io/2018/04/03/LayoutInflate%E9%83%A8%E5%88%86%E6%BA%90%E7%A0%81%E8%A7%A3%E6%9E%90/

许可协议: 署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。