LayoutInflater的作用是将xml构建成view
1、LayoutInflater使用
一般常见的比如Fragment的onCreateView
或者ListView,RecyclerView中等。
LayoutInflater.from(MainActivity.this).inflate();
而inflate
有以下重载方法。
/**
* @params resource 布局资源文件
* @params root 布局根目录
* @attachToRoot 是否依附于根目录
*/
public View inflate(@LayoutRes int resource, @Nullable ViewGroup root, boolean attachToRoot)
public View inflate(@LayoutRes int resource, @Nullable ViewGroup root)
public View inflate(XmlPullParser parser, @Nullable ViewGroup root)
其中两个是直接使用R.layout.xxx
文件,剩下的一个是直接使用pull解析出来的对象。
2、LayoutInflater对象怎么创建
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;
}
可以看到LayoutInflater
是直接使用getSystemService
的形式获取的,至于SystemService
这块知识暂时不做说明,后续用专门篇幅讲解。
3、分析LayoutInflater是如何从xml文件生成View
在讲解LayoutInflater.inflate
之前,先插播一下xmlPullParser
的相关知识,方便后面理解。
XMLPULL的一些特点:
简单的接口:XMLPULL由一个接口(XmlPullParser)、一个例外(XmlPullParserException)、一个工厂(XmlPullParserFactory)来创建。
- 易用性:只有一个关键的next()方法,用于检索下一事件。
- 易扩展:使用通用的接口,并允许多个实现功能,具有更好的扩展性。
- 性能:XMLPULL被设计为允许执行速度非常快的XML解析器。
- 内存要求低:XMLPULL被设计为兼容J2ME,在小型设备上,解析XML时占用非常小的内存。
对于XMLPULL,只有一个关键的next()方法需要了解一下,它是用于检索下一个事件,并有五个事件,这五个分别是:
- START_DOCUMENT:文档的开始,解析器尚未读取任何输入。
- START_TAG:开始标签的解析。
- TEXT:标签内元素的内容解析。
- END_TAG:结束标签的解析。
- END_DOCUMENT:文档的结束。
虽然说关键方法只有一个用于检索下一事件的方法next(),但是还存在一些方法也可以检索下一事件,用于不同的情况下使用,如:nextText():用于检索下一元素的文本;nextTag():用于检索下一元素的标签。
XmlPullParser
是使用XmlPullParserFactory
的newPullParser
构造出来的,而XmlPullparserFactory
是单利构造,必须使用newInstance()
获取。
XmlPullParser parser = XmlPullParserFactory.newInstance().newPullParser();
接下来进入正题,LayoutInflate#inflate
。
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();
}
}
可以看到通过Resource#getLayout
方法构建出来了一个XmlResourceParse
对象,之后调用inflate
方法,分别传入了解析器对象、根布局、是否附加到根布局。XmlResourceParser
是XmlPullParser
的子类,所以布局解析就是由XmlResourceParser
执行的。
public View inflate(XmlPullParser parser, @Nullable ViewGroup root, boolean attachToRoot)
public View inflate(XmlPullParser parser, @Nullable ViewGroup root, boolean attachToRoot) {
synchronized (mConstructorArgs) {
View result = root;
...
final String name = parser.getName();
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) {
...
params = root.generateLayoutParams(attrs);
if (!attachToRoot) {
temp.setLayoutParams(params);
}
}
rInflateChildren(parser, temp, attrs, true);
if (root != null && attachToRoot) {
root.addView(temp, params);
}
if (root == null || !attachToRoot) {
result = temp;
}
}
...
return result;
}
}
代码中省去了非主要逻辑代码,可以看到获取到节点名称,如果节点是Merge
节点的话,会判断root节点是是否为空,因为Merge
节点的布局是不能直接用的,只能include
到其他布局中使用。这里将逻辑分开看,以merge为分界。
- merge类型文件解析
void rInflate(XmlPullParser parser, View parent, Context context,
AttributeSet attrs, boolean finishInflate) throws XmlPullParserException, IOException {
final int depth = parser.getDepth();
int type;
boolean pendingRequestFocus = false;
while (((type = parser.next()) != XmlPullParser.END_TAG ||
parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
if (type != XmlPullParser.START_TAG) {
continue;
}
final String name = parser.getName();
if (TAG_REQUEST_FOCUS.equals(name)) {
pendingRequestFocus = true;
consumeChildElements(parser);
} else if (TAG_TAG.equals(name)) {
parseViewTag(parser, parent, attrs);
} else if (TAG_INCLUDE.equals(name)) {
if (parser.getDepth() == 0) {
throw new InflateException("<include /> cannot be the root element");
}
parseInclude(parser, context, parent, attrs);
} else if (TAG_MERGE.equals(name)) {
throw new InflateException("<merge /> must be the root element");
} else {
//创建view的主要操作,内部最终使用classLoader加载生成类对象。
final View view = createViewFromTag(parent, name, context, attrs);
final ViewGroup viewGroup = (ViewGroup) parent;
final ViewGroup.LayoutParams params = viewGroup.generateLayoutParams(attrs);
// 这里实际上还是调用了rInflate,形成了一个递归调用,根绝树的深度进行便利。
rInflateChildren(parser, view, attrs, true);
//将遍历出来的布局加入到跟布局上,params负责控制布局的加入参数,比如宽高、相对位置等。
viewGroup.addView(view, params);
}
}
if (pendingRequestFocus) {
parent.restoreDefaultFocus();
}
if (finishInflate) {
parent.onFinishInflate();
}
}
- 标准型文件解析
final View temp = createViewFromTag(root, name, inflaterContext, attrs);
ViewGroup.LayoutParams params = null;
if (root != null) {
...
params = root.generateLayoutParams(attrs);
if (!attachToRoot) {
temp.setLayoutParams(params);
}
}
rInflateChildren(parser, temp, attrs, true);
if (root != null && attachToRoot) {
root.addView(temp, params);
}
if (root == null || !attachToRoot) {
result = temp;
}
先看createViewFromTag
,这个应该是创建出来一个View
对象,看一下内部代码。
View createViewFromTag(View parent, String name, Context context, AttributeSet attrs,
boolean ignoreThemeAttr) {
try {
View view;
if (mFactory2 != null) {
view = mFactory2.onCreateView(parent, name, context, attrs);
} else if (mFactory != null) {
view = mFactory.onCreateView(name, context, attrs);
} else {
view = null;
}
if (view == null && mPrivateFactory != null) {
view = mPrivateFactory.onCreateView(parent, name, context, attrs);
}
if (view == null) {
try {
if (-1 == name.indexOf('.')) {
view = onCreateView(parent, name, attrs);
} else {
view = createView(name, null, attrs);
}
} finally {
mConstructorArgs[0] = lastContext;
}
}
return view;
} catch (InflateException e) {
throw e;
} catch (ClassNotFoundException e) {
final InflateException ie = new InflateException(attrs.getPositionDescription()
+ ": Error inflating class " + name, e);
ie.setStackTrace(EMPTY_STACK_TRACE);
throw ie;
} catch (Exception e) {
final InflateException ie = new InflateException(attrs.getPositionDescription()
+ ": Error inflating class " + name, e);
ie.setStackTrace(EMPTY_STACK_TRACE);
throw ie;
}
}
直接看if (view == null) {}
这块代码,因为mFactory2
和mFactory
是用来扩展的,这里肯定View
为空。
if (-1 == name.indexOf('.')) {
// 这里判断分支,如果名称不包含.的话,则会设置prefix为```android.view.```
view = onCreateView(parent, name, attrs);
} else {
//如果包含了.,则应该就是我们自定义的视图或者support包提供的布局
view = createView(name, null, attrs);
}
protected View onCreateView(String name, AttributeSet attrs)
throws ClassNotFoundException {
return createView(name, "android.view.", attrs);
}
在看createView
代码,
...
//通过classLoader加载类信息
clazz = mContext.getClassLoader().loadClass(
prefix != null ? (prefix + name) : name).asSubclass(View.class);
constructor = clazz.getConstructor(mConstructorSignature);
constructor.setAccessible(true);
}
//得到一个临时的Root节点对象
final View view = constructor.newInstance(args);
至此createViewFromTag
分析完了,接着看if (root != null)
则设置布局参数给root。在看rInflateChildren
发现里面结构也是rInflate
所以和merge结构一样的,之后判断了root不为空,并且设置attachToRoot为true的话,则执行root.addView(temp, params);
,如果root为空,并且attachToRoot为false,则将createViewFromTag获取的临时root设置为root节点。
在所有的布局遍历完成之后,会执行会调parent.onFinishInflate();
表示从xml到view的所有遍历工作已经完成。
- 不知道现在你脑子里面有没有一个疑问,既然
createView
方法在处理系统的视图的时候传入的是android.view.
,但是如果你看LinearLayout
这些的话,他们应该是android.weight.
,那么是怎么创建出来的呢?
其实这个问题我也思考了很久,终于在翻阅别人对LayoutInflater
分析的留言区里找到回复,说是在PhoneWindow,然后在PhoneWindow中的LayoutInflater实际上使用的是PhoneLayoutInflater,也就是对LayoutInflater进行了继承扩展。
public class PhoneLayoutInflater extends LayoutInflater {
private static final String[] sClassPrefixList = {
"android.widget.",
"android.webkit.",
"android.app."
};
/**
* Instead of instantiating directly, you should retrieve an instance
* through {@link Context#getSystemService}
*
* @param context The Context in which in which to find resources and other
* application-specific things.
*
* @see Context#getSystemService
*/
public PhoneLayoutInflater(Context context) {
super(context);
}
protected PhoneLayoutInflater(LayoutInflater original, Context newContext) {
super(original, newContext);
}
/** 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);
}
public LayoutInflater cloneInContext(Context newContext) {
return new PhoneLayoutInflater(this, newContext);
}
}
在onCreateView
的sClassPrefixList
列表中,定义了android.widget.
。好了至此算是真的结束了。
参考:
https://www.cnblogs.com/plokmju/p/android_XMLForPull.html
https://blog.youkuaiyun.com/l540675759/article/details/78099702