从LayoutInflater分析XML布局解析成View的树形结构的过程
上一篇博客分析了XML布局怎么载入到Activity上。不了解的能够參考
从setContentView方法分析Android载入布局流程
上一篇博客仅仅是分析了怎么讲XML布局加入到 Activity 的DecorView根布局上,最后是通过例如以下代码将资源布局加入到Activity上
mLayoutInflater.inflate(layoutResID, mContentParent);
參考博客从setContentView方法分析Android载入布局流程 Step3 第 17行。
Step1
那么inflate方法里面详细做了什么?跟踪代码。该方法的实现是在LayoutInflater类中。
public View inflate(int resource, ViewGroup root) {
return inflate(resource, root, root != null);
}
该方法非常easy。方法体里面直接调用 例如以下方法
public View inflate(int resource, 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();
}
}
代码的第 8 行,调用XML解析器将xml资源解析成XmlResourceParser对象作为參数传
递给第10行 inflate(parser, root, attachToRoot);方法。
该方法实现例如以下:
Step2
public View inflate(XmlPullParser parser, ViewGroup root, boolean attachToRoot) {
synchronized (mConstructorArgs) {
final AttributeSet attrs = Xml.asAttributeSet(parser);
Context lastContext = (Context)mConstructorArgs[0];
mConstructorArgs[0] = mContext;
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 (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, attrs, false, false);
} else {
// Temp is the root view that was found in the xml
final View temp = createViewFromTag(root, name, attrs, false);
ViewGroup.LayoutParams params = null;
if (root != null) {
// 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);
}
}
// Inflate all children under temp
rInflate(parser, temp, attrs, true, 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) {
InflateException ex = new InflateException(e.getMessage());
ex.initCause(e);
throw ex;
} catch (IOException 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资源的根节点来创建一个 root view 。
1.代码第12-15行,while循环中是一个Empty操作。目的是遍历查找当前xml资源文件的根接点。
2.代码第17-20行进行if推断,假设没有找到xml资源的根接点。则抛出一个异常,说明当前载入的xml资源布局出错。
3.代码第24-31行推断当前xml资源的根接点是否是 < merge /> ,从第25行的if推断能够看出,使用< merge />标签的根布局须要满足 root。=null && attachToRoot 两个条件。
4.假设当前标签不是 < merge />则运行第33行代码createViewFromTag方法。该方法依据当前根节点的名称和属性名创建一个 root View。
5.第48行代码调用 rInflate(parser, temp, attrs, true, true); 方法将所有的子 view 载入到 root view以下。
注意 无论根标签是否是 < merge />,最后都会调用rInflate方法来遍历载入子元素view。
我们来看看 rInflate方法的实现
Step3
void rInflate(XmlPullParser parser, View parent, final AttributeSet attrs,
boolean finishInflate, boolean inheritContext) throws XmlPullParserException,
IOException {
final int depth = parser.getDepth();
int type;
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)) {
parseRequestFocus(parser, parent);
} 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, parent, attrs, inheritContext);
} else if (TAG_MERGE.equals(name)) {
throw new InflateException("<merge /> must be the root element");
} else {
final View view = createViewFromTag(parent, name, attrs, inheritContext);
final ViewGroup viewGroup = (ViewGroup) parent;
final ViewGroup.LayoutParams params = viewGroup.generateLayoutParams(attrs);
rInflate(parser, view, attrs, true, true);
viewGroup.addView(view, params);
}
}
if (finishInflate) parent.onFinishInflate();
}
以上方法就是遍历xml资源根布局 root view 下的子元素。而且将子元素view依次加入到 root view以下。
1.代码第17-28做一系列的推断处理,有 21-27行推断可知。< include/>标签是不能作为 xml资源布局的 root view 布局的根节点的,还有 < merge/>是必须作为 xml资源的根布局 root view的根节点才干够使用的。
2.代码第29行,依据当前view的节点名称和属性调用 createViewFromTag 方法创建子元素 view。
3.代码32行。递归调用当前方法。查找当前view 以下的子元素view。
4.代码第33行,将当前 view以下的之元素view加入到 view上面,然后循环。直到所有子元素载入完成。
至此,整个xml资源布局解析绘制成view的过程就结束了。附上流程图:
LayoutInflater解析XML资源流程
总结: LayoutInflater类给开发人员暴露了两个方法用于载入布局
1.
public View inflate(int resource, ViewGroup root)
2.
public View inflate(int resource, ViewGroup root, boolean attachToRoot)
分析
- 当root=null时,attachToRoot不起不论什么作用。
- 当root!=null时。attachToRoot=false时,xml资源布局不加入到root根布局下,也就是root失效。
- 当root!=null,attachToRoot=true时,xml资源布局会加入到root根布局下。
- 在调用第一种方法。没有attachToRoot參数时,当root=null时的情况和第一种分析一样。当root!=null的情况和第三那种分析一样。
最后XML布局解析成例如以下的View树形结构图
从LayoutInflater分析XML布局解析成View的树形结构的过程的更多相关文章
- xml布局解析报错的可能原因
xml布局解析报如下的错11-15 16:55:21.425 17633-17633/com.hongfans.mobileconnect I/LogUtils_info: [CrashHandler ...
- js 将XML字符串解析成XML文档 --- attribute construct error--- 空白字符与空格问题
最近在做xml在线编辑器,遇到一个字符串解析成xml文档的问题,记录一下. 原始xml内容读取自xml文档 <label class="test" id="labe ...
- 【转】xml节点解析成字符串的方法
网址:http://blog.csdn.net/shanzhizi/article/details/8817532 ZC: 这是 libxml2的 之前汇总了一篇关于xml文档与字符串转换的文章,文章 ...
- xml 字符串解析成通用的map
[quote]Stax解析技术:快速高效,根据流的形式解析xml,比dom解析技术更加快,dom解析技术是基于文档结构树的,会把整个dom文件树加载到内存进行解析[/quote] package co ...
- js将xml对象,xml文件解析成xml dom对象,来对对象进行操作
由于ie与其他的浏览器对于xml文件的解析方式不同,所以有不同的解析方式 1.1 IE解析xml文件的方式 var xmlDoc=new ActiveXObject("Microsoft.X ...
- XML字符串解析成对象的时候应注意空格
BomList bomList=(BomList)unmarshaller_bom.unmarshal(new StringReader(xml));xml 不能以空格开头
- [ java 工具类] xml字符串解析成Map(DOM解析)
package com.tencent.jungle.wechat.util; import com.google.inject.Singleton; import org.w3c.dom.Docum ...
- Android中measure过程、WRAP_CONTENT详解以及 xml布局文件解析流程浅析
转自:http://www.uml.org.cn/mobiledev/201211221.asp 今天,我着重讲解下如下三个内容: measure过程 WRAP_CONTENT.MATCH_PAREN ...
- Android学习笔记_31_通过后台代码生成View对象以及动态加载XML布局文件到LinearLayout
一.布局文件part.xml: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android&qu ...
随机推荐
- select自定义小三角样式
这段代码是网上大部分的解决办法,在这里总结一下: 让select透明,上面加一个span,来替换select框,可以自定义小三角样式,也可以做出select文字居中的效果. <div class ...
- 使用 MVVMLight 命令绑定
首先,如果您希望了解更多的MVVMLight技术或希望有顺序的学习MVVMLight,请查阅目录<MVVMLight 设计模式系列使用文章>. 继上一篇文章的项目,我们实现了数据绑定到界面 ...
- Cocos2d-x 3.0 Lua编程 之 响应Android手机的按键
演示样例代码例如以下所看到的: local listenerKey= cc.EventListenerKeyboard:create() local function onKeyReleaseed(k ...
- 从头认识java-16.5 nio的数据转换
这一章节我们来讨论一些nio的数据转换. 上面一章节我们提到ByteBuffer,可是他是面向二进制数据,对于编程来说不是非常方便,因此,java添加了转换数据的工具. 1.asCharBuffer ...
- Java 基本语法----数组
数组 数组概述 数组是多个相同类型数据的组合,实现对这些数据的统一管理. 数组属引用类型,数组型数据是对象(Object),数组中的每个元素相当于该对象的成员变量. 数组中的元素可以是任何数据类型,包 ...
- POJ 1141 Brackets Sequence(区间DP, DP打印路径)
Description We give the following inductive definition of a “regular brackets” sequence: the empty s ...
- swift开发之--报错:Class "***ViewController" has no initializers
因为Swift中要求变量或常量在声明时就要初始化其值,所以我们在实际开发中,声明变量或常量时使用可选类型. ?!1234 var stitle : UILabel?var webview : UIWe ...
- ContentPriver
共享应用程序内的数据, 在数据修改时可以监听 1.特点 ①.可以将应用中的数据对外进行共享: ②.数据访问方式统一,不必针对不同数据类型采取不同的访问策略: ③.内容提供者将数据封装,只暴露出我们希望 ...
- unix:/tmp/php-cgi.sock
为什么要用unix:/tmp/php-cgi.sock,最主要的特征就是unix socket比tcp快,当网站流量大的时候,服务器的优化是分毫必争的. 当我们用php-fpm来管理我们的php启动时 ...
- 关于font-size对垂直居中影响的问题
背景:三个inline-block元素,其中两个内容为空,另外一个包含文字,设置文字的font-size之后,原本垂直居中的三个inline-block的元素,会变的不再垂直居中. 原因: 当设置了f ...