LayoutInflater的传参问题

Android开发中,我们会经常需要把一个xml解析成一个View。最常见的就是在Adapter的getView方法中通过LayoutInflater把一个布局文件解析成一个View。但是,这里经常会出现一些问题,比如我在item布局文件的根布局中设置的属性经常会没效果等等。其实,在我们在Activity的onCreate方法中调用Activity的setContentView方法给Activity设置布局的时候,底层也是通过LayoutInflater来将传入的布局解析成View然后在添加到界面上的。所以,为了搞清楚LayoutInflater的工作原理,我翻了翻源码,基本上把问题搞明白了。

先来看一个问题:在给ListView填充item布局时,以下几种填充布局的方法有什么异同?

item的布局文件长这样,很简单

1
2
3
4
5
6
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="100dp"
android:layout_height="50dp"
android:gravity="center"
android:background="@android:color/holo_green_light"/>

1
2
3
4
5
6
7
8
9
10
11
① convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_list, parent,false);

② convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_list, parent,true);

③ convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_list, null,false);

④ convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_list, null,true);

⑤ convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_list, parent);

⑥ convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_list, null);

先把答案公布了,后面咱通过源码来找原因。

①的效果图:

②和⑤的效果图:

③④⑥的效果图:

好了,下面我们从源码开始找找原因。故事的起因源自LayoutInflater.from().inflate(),那么我们就从这里入手。

1
2
3
4
5
6
7
8
9
10
11
 /**
* Obtains the LayoutInflater from the given context.
*/

public static LayoutInflater from(Context context) {
LayoutInflater LayoutInflater =
(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (LayoutInflater == null) {
throw new AssertionError("LayoutInflater not found.");
}
return LayoutInflater;
}

from()方法先通过context来获取一个系统服务LAYOUT_INFLATER_SERVICE,由于这里的LayoutInflater是个抽象方法,所以这里其实最终得到的是子类PhoneLayoutInflater的实例,PhoneLayoutInflater复写了父类的onCreateView()方法。

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
private static final String[] sClassPrefixList = {
"android.widget.",
"android.webkit."
};

/** Override onCreateView to instantiate names that correspond to the
widgets known to the Widget factory. If we don't find a match,
call through to our super class.
*/

@Override
protected View onCreateView(String name, AttributeSet attrs) throws ClassNotFoundException {
for (String prefix : sClassPrefixList) {
try {
View view = createView(name, prefix, attrs);
if (view != null) {
return view;
}
} catch (ClassNotFoundException e) {
// In this case we want to let the base class take a crack
// at it.
}
}

return super.onCreateView(name, attrs);
}

这里PhoneLayoutInflater只是把系统控件的包名前缀传给createView(),然后createView()就会解析这个控件标签返回这个控件的实例。

所以,现在我们知道通过LayoutInflater.from()方法返回的其实是PhoneLayoutInflater的实例。然后再调用inflate()方法完成真正的布局填充工作。所以,可以猜到inflate()里面有我们的答案。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
/**
* Inflate a new view hierarchy from the specified xml resource. Throws
* {@link InflateException} if there is an error.
*
* @param resource ID for an XML layout resource to load (e.g.,
* <code>R.layout.main_page</code>)
* @param root Optional view to be the parent of the generated hierarchy.
* @return The root View of the inflated hierarchy. If root was supplied,
* this is the root View; otherwise it is the root of the inflated
* XML file.
*/

public View inflate(@LayoutRes int resource, @Nullable ViewGroup root) {
return inflate(resource, root, root != null);
}

两个参数的方法直接调用三个参数的方法。

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
/**
* Inflate a new view hierarchy from the specified xml resource. Throws
* {@link InflateException} if there is an error.
*
* @param resource ID for an XML layout resource to load (e.g.,
* <code>R.layout.main_page</code>)
* @param root Optional view to be the parent of the generated hierarchy (if
* <em>attachToRoot</em> is true), or else simply an object that
* provides a set of LayoutParams values for root of the returned
* hierarchy (if <em>attachToRoot</em> is false.)
* @param attachToRoot Whether the inflated hierarchy should be attached to
* the root parameter? If false, root is only used to create the
* correct subclass of LayoutParams for the root view in the XML.
* @return The root View of the inflated hierarchy. If root was supplied and
* attachToRoot is true, this is root; otherwise it is the root of
* the inflated XML file.
*/

public View inflate(@LayoutRes int resource, @Nullable ViewGroup root, boolean attachToRoot) {
final Resources res = getContext().getResources();

final XmlResourceParser parser = res.getLayout(resource);
try {
return inflate(parser, root, attachToRoot);
} finally {
parser.close();
}
}

这里先获取到一个Resources对象,然后通过我们传进来的资源id生成一个XmlResourceParser对象,熟悉xml解析的人对这个应该不陌生。至此我们的xml的信息都装在了XmlResourceParser对象中,然后再调用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
public View inflate(XmlPullParser parser, @Nullable ViewGroup root, boolean attachToRoot) {
synchronized (mConstructorArgs) {
final Context inflaterContext = mContext;
final AttributeSet attrs = Xml.asAttributeSet(parser);
Context lastContext = (Context) mConstructorArgs[0];
mConstructorArgs[0] = inflaterContext;
View result = root;

try {
// Look for the root node.
//寻找根布局
int type;

final String name = parser.getName();//拿到标签名 比如TextView

if (TAG_MERGE.equals(name)) {//解析merge标签
rInflate(parser, root, inflaterContext, attrs, false);
} else {
// temp是在xml中解析出来的根布局. 这里是指根据标签名解析了该标签,并没有解析子标签
final View temp = createViewFromTag(root, name, inflaterContext, attrs);

ViewGroup.LayoutParams params = null;

if (root != null) {
//可以看到,只有root即我们指定的parent不为null的时候,才会使用到根布局中指定的布局参数。**这里就解释了为什么③④⑥中我们的高宽值没有生效**
params = root.generateLayoutParams(attrs);
if (!attachToRoot) {
//attachToRoot为false时,才会给我们xml中的跟布局设置布局参数,**这里解释了为什么①的效果跟我们预期相同**
// Set the layout params for temp if we are not
// attaching. (If we are, we use addView, below)
temp.setLayoutParams(params);
}
}

// Inflate all children under temp against its context.
//这里递归调用,解析temp的所有子控件并把解析的子控件添加到temp中
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即parent不为null且attachToRoot为true的话,就把我们xml解析出来的布局添加到root中
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) {
//这里可以看到,如果root即parent为null或者attachToRoot为false,就直接把xml的跟布局返回。也就是把xml中的根布局作为根布局
result = temp;
}
}

} catch (XmlPullParserException e) {

} finally {

}

return result;
}
}

至此,我们从源码中找到了①③④⑥不同效果的原因。至于为什么②和⑤会导致应用崩掉,这是因为ListView不支持addView的操作。如果root即parent不为null且attachToRoot为true(这个条件正是②和⑤给的参数)时,会走这行代码root.addView(temp, params),但ListView不支持addView,抛了异常。

1
2
3
4
5
6
7
8
9
10
11
12
/**
* This method is not supported and throws an UnsupportedOperationException when called.
*
* @param child Ignored.
* @param index Ignored.
*
* @throws UnsupportedOperationException Every time this method is invoked.
*/

@Override
public void addView(View child, int index) {
throw new UnsupportedOperationException("addView(View, int) is not supported in AdapterView");
}

可以看到,如果调用listView的addView方法,直接就抛异常了。当然,这只是listView的特殊情况而已。

我们现在搞明白了开头提到的问题,也清楚了当使用LayoutInflater.from().inflate()方法时应该传递怎样的参数才能达到我们预期的效果。至于inflate解析xml的过程,我们下一篇再说。

参考

鸿洋博客

Android源码设计模式解析与实践