上次讲到以下这么一段代码,这段代码的作用就是解析xml文件成为view并显示到屏幕上的。

 @Override
//设置contentview,也就是activity或fragment载入视图,即view的函数。接受的參数是资源id
public void setContentView(int layoutResID) {
//mContentParent是个ViewGroup ,这里mContentParent == null肯定是成立的
if (mContentParent == null) {
//成立后就会执行这一句话,从字面意思就能够看出,是安装解析器来着
installDecor();
} else {
mContentParent.removeAllViews();
}
//这一句话開始解析并初始化资源了
mLayoutInflater.inflate(layoutResID, mContentParent);
//取得对应的回调
final Callback cb = getCallback();
if (cb != null) {
//一旦有回调被调用了,就要通知视图改变
cb.onContentChanged();
}
}

纵观上面的一段代码,可能最实用的无非这么一句

 mLayoutInflater.inflate(layoutResID, mContentParent);

他解析了id为layoutResId的资源。

mLayoutInflater这个成员变量,说起这个东西。又是一堆废话要讲。大家千万不要嫌我啰嗦。由于终于我会回到一个很实用的主题。在阐述这个主题之前。这些“小知识”都是必备的,就比方说爱爱之前总要来点前戏对吧

假设你听说过Fragment,那么应该知道fragment中载入视图是通过类似

public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
View layout = inflater.inflate(R.layout.main_activity_menu, null);
initLayouView(layout);
return layout;
}

这种代码实现的。

这就是inflate的作用。他就是用来解析xml并显示到屏幕的,至于解析的代码我这边就不多做阐述了,毕竟已经超越了我们的主题太远。我们还是看inflate是怎样给解析的xml分配内存。并加入到view上的吧!

这里是

v=source">LayoutInflater.java的源码

当中inflate函数的实如今这里:

    public View inflate(XmlPullParser parser, ViewGroup root, boolean attachToRoot) {
synchronized (mConstructorArgs) {
final AttributeSet attrs = Xml.asAttributeSet(parser);
mConstructorArgs[0] = mContext;
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!");
}
//这里取得XML标签名字,比方说LinearLayout
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);
} else {
// 在此处创建视图,參数是前面取得的name,也就是说取得了LinearLayout開始new这个对象了! 第二个參数就是这个视图的属性
View temp = createViewFromTag(name, attrs); //下面的代码省略......

以上的代码中。注意这一行:

 View temp = createViewFromTag(name, attrs);

这一行就是通过解析tag来创建view的,这种方法的实现例如以下:

//这里的整个过程是:先推断是不是view,假设是的话。再取得属性值
View createViewFromTag(String name, AttributeSet attrs) {
if (name.equals("view")) {
name = attrs.getAttributeValue(null, "class");
} try {//这里的mFactory是一个叫做Factory的interface,在Inflater类构造的时候新建了mFactory对象
View view = (mFactory == null) ? null : mFactory.onCreateView(name,
mContext, attrs); if (view == null) {
if (-1 == name.indexOf('.')) {
view = onCreateView(name, attrs);
} else {
view = createView(name, null, attrs);
}
} //下面代码省略...

再关注里面的

                    view = createView(name, null, attrs);

这句话,正式開始创建了!

public final View createView(String name, String prefix, AttributeSet attrs)
throws ClassNotFoundException, InflateException {
//这里取得了xml的标签,比方说LinearLayout,这个构造器类是通过java的反射获取对象以及參数
Constructor constructor = sConstructorMap.get(name);
Class clazz = null; try {
if (constructor == null) {
// 这个是我们熟悉的类载入器。是另外一种初始化对象的方法(第一种是直接new出来)
clazz = mContext.getClassLoader().loadClass(
prefix != null ? (prefix + name) : name); if (mFilter != null && clazz != null) {
boolean allowed = mFilter.onLoadClass(clazz);
if (!allowed) {
failNotAllowed(name, prefix, attrs);
}
}
constructor = clazz.getConstructor(mConstructorSignature);
sConstructorMap.put(name, constructor);
} else {
// If we have a filter, apply it to cached constructor
if (mFilter != null) {
// Have we seen this name before?
Boolean allowedState = mFilterMap.get(name);
if (allowedState == null) {
// New class -- remember whether it is allowed
clazz = mContext.getClassLoader().loadClass(
prefix != null ? (prefix + name) : name); boolean allowed = clazz != null && mFilter.onLoadClass(clazz);
mFilterMap.put(name, allowed);
if (!allowed) {
failNotAllowed(name, prefix, attrs);
}
} else if (allowedState.equals(Boolean.FALSE)) {
failNotAllowed(name, prefix, attrs);
}
}
} Object[] args = mConstructorArgs;
args[1] = attrs;
//上面取得了view,在这里又取得了attrs,换句话说,既取得了对象(比方说LinearLayout),又取得了參数(比方说高度,宽度等等)
return (View) constructor.newInstance(args);//下面代码省略...

创建成功了!

LinearLayout具体解释三:LayoutInflater创建View过程分析的更多相关文章

  1. Js基础知识4-函数的三种创建、四种调用(及关于new function()的解释)

    在js中,函数本身属于对象的一种,因此可以定义.赋值,作为对象的属性或者成为其他函数的参数.函数名只是函数这个对象类的引用. 函数定义 // 函数的三种创建方法(定义方式) function one( ...

  2. 2019年6月14日 Web框架之Django_07 进阶操作(MTV与MVC、多对多表三种创建方式、前后端传输数据编码格式contentType、ajax、自定义分页器)

    摘要 MTV与MVC 多对多表三种创建方式 ajax ,前后端传输数据编码格式contentType 批量插入数据和自定义分页器 一.MVC与MTV MVC(Model View Controller ...

  3. Android使用默认样式创建View的几个姿势

    以下内容是分析安卓源码所得: 1: 使用默认样式创建View的方式, 源码文件 Button.Java  注:此文参考http://www.linzenews.com/ 中的内容所写,如侵删! 2: ...

  4. 使用Java代码来创建view

    使用Java代码来创建view 一.简介 需要了解的知识 二.方法 1)java代码创建view方法 * 1.先建view对象 View view= View.inflate(this, R.layo ...

  5. Django-多对多关系的三种创建方式-forms组件使用-cookie与session-08

    目录 表模型类多对多关系的三种创建方式 django forms 组件 登录功能手写推理过程 整段代码可以放过来 forms 组件使用 forms 后端定义规则并校验结果 forms 前端渲染标签组件 ...

  6. geotrellis使用(三)geotrellis数据处理过程分析

    之前简单介绍了geotrellis的工作过程以及一个简单的demo,最近在此demo的基础上实现了SRTM DEM数据的实时分析以及高程实时处理,下面我就以我实现的上述功能为例,简单介绍一下geotr ...

  7. Fixflow引擎解析(三)(模型) - 创建EMF模型来读写XML文件

    Fixflow引擎解析(四)(模型) - 通过EMF扩展BPMN2.0元素 Fixflow引擎解析(三)(模型) - 创建EMF模型来读写XML文件 Fixflow引擎解析(二)(模型) - BPMN ...

  8. VMware vSphere 服务器虚拟化之二十二桌面虚拟化之创建View Composer链接克隆的虚拟桌面池

    VMware vSphere 服务器虚拟化之二十二桌面虚拟化之创建View Composer链接克隆的虚拟桌面池 在上一节我们创建了完整克隆的自动专有桌面池,在创建过程比较缓慢,这次我们将学习创建Vi ...

  9. IOS程序创建view

    在IOS程序中创建view有六种方式 首先创建一个GLViewController类,继承UIViewController. 然后进入GLAppDelegate.m,在- (BOOL)applicat ...

随机推荐

  1. 0016.Linux基础之常用基本命令讲解

    开启linux系统,开启xshell pwd:printing workding directory 打印当前目录 /:代表根目录 cd:change directory 改变目录 ls:list 查 ...

  2. 紫书第三章训练1 D - Crossword Answers

    A crossword puzzle consists of a rectangular grid of black and white squares and two lists of defini ...

  3. Mysql读写分离实例

    吐槽:前天刚加完MQ,这回加读写分离.我也是醉了,但是弄完之后,就发现,似乎没我想的那么复杂,真的!另外,昨天试了一下用swagger编写API文档,太方便了,加上Mock service测试.这两天 ...

  4. 九度oj 题目1465:最简真分数

    题目描述: 给出n个正整数,任取两个数分别作为分子和分母组成最简真分数,编程求共有几个这样的组合. 输入: 输入有多组,每组包含n(n<=600)和n个不同的整数,整数大于1且小于等于1000. ...

  5. map/set/object/array对比

    map () { //数据结构横向对比, 增,查,改,删 let map = new Map() let array = [] //增 map.set('t',1) array.push({t:1}) ...

  6. 【Luogu】P2515软件安装(树形DP)

    题目链接 这么水的题我一遍没A,而且前两次提交都只有十分.气死我了.本来我的博客拒收水题来着. Tarjan缩点之后跑树形DP即可. #include<cstdio> #include&l ...

  7. spring分布式事务控制

    应用场景问题描述解决方法多数据源配置单元测试第一种方法:最大努力一次提交模式第二种方法:最大努力一次提交模式 但使用ChainedTransactionManagerChainedTransactio ...

  8. C# 模拟windows文件名称排序(使用windows自带dll)

    [DllImport("shlwapi.dll", CharSet = CharSet.Unicode)] private static extern int StrCmpLogi ...

  9. vue 配合 element-ui使用搭建环境时候遇到的坑

    在需要使用element-ui的时候,直接引入文件,发现会报错,解析不了css文件和字体,需要在webpack里面配置上css-loader和style-loader,最好的做法是把element-u ...

  10. bzoj 2803 [POI2012]prefixuffix hsh+性质

    题目大意 bzoj 2803 对于两个串S1.S2,如果能够将S1的一个后缀移动到开头后变成S2,就称S1和S2循环相同.例如串ababba和串abbaab是循环相同的. 给出一个长度为n的串S,求满 ...