
Android的Inflater使用
发布日期:2021-05-10 06:29:16
浏览次数:28
分类:原创文章
本文共 4590 字,大约阅读时间需要 15 分钟。
LayoutInflater 的使用方式:
方式一:
方式二:
方式三:
三种方式效果一样,返回View对象
参数的作用
- resource:Int
需要加载的XML布局资源的ID - root:ViewGroup
指定一个父View - attachToRoot:boolean
是否添加到root中
很明显,主要的决定条件就是root:ViewGroup,attachToRoot:boolean这两个参数:
- attachToRoot:boolean为空,则默认为true
- root不为null,attachToRoot设为false,此时root会协助需要加载的XML布局资源进行根节点的layout属性进行设值,但不会加载进root下
- root不为null,attachToRoot设为true,即将需要加载的XML布局资源添加到root中
- 当root为null,attachToRoot将失去作用,即不添加到root中,此时需要加载的XML布局资源的根节点的layout属性是没有的,如果我们将该布局添加到某一个容器中,怎么在该布局的根节点进行设置宽高都是没有效果的,但是在根节点的子控件设值却可以改变布局宽高,这是因为子控件是有父布局的呀。
源码
进入inflate():
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) + ")"); } //检查是否有预编译,如果有直接使用 View view = tryInflatePrecompiled(resource, res, root, attachToRoot); if (view != null) { return view; } //解析xml文件得到XmlResourceParser对象 XmlResourceParser parser = res.getLayout(resource); try { //调用重载 return inflate(parser, root, attachToRoot); } finally { parser.close(); }}
调用重载:
public View inflate(XmlPullParser parser, @Nullable ViewGroup root, boolean attachToRoot) { synchronized (mConstructorArgs) { Trace.traceBegin(Trace.TRACE_TAG_VIEW, "inflate"); final Context inflaterContext = mContext; final AttributeSet attrs = Xml.asAttributeSet(parser); Context lastContext = (Context) mConstructorArgs[0]; mConstructorArgs[0] = inflaterContext; View result = root; try { //判断布局的合理性 advanceToRootNode(parser); final String name = parser.getName(); if (DEBUG) { System.out.println("**************************"); System.out.println("Creating root view: " + name); System.out.println("**************************"); } //如果根节点是Merge标签 if (TAG_MERGE.equals(name)) { //root必须不为空,且attachToRoot为true, //也就是必须添加到root中,不然报异常 if (root == null || !attachToRoot) { throw new InflateException("<merge /> can be used only with a valid " + "ViewGroup root and attachToRoot=true"); } //调用rInflate()方法解析 rInflate(parser, root, inflaterContext, attrs, false); } else { //根据节点名来创建View对象 final View temp = createViewFromTag(root, name, inflaterContext, attrs); ViewGroup.LayoutParams params = null; if (root != null) { if (DEBUG) { System.out.println("Creating params from root: " + root); } //root != null则根据当前标签生成LayoutParams params = root.generateLayoutParams(attrs); if (!attachToRoot) { //如果attachToRoot==false,则把LayoutParams信息设值到根节点上 temp.setLayoutParams(params); } } if (DEBUG) { System.out.println("-----> start inflating children"); } //调用rInflateChildren() rInflateChildren(parser, temp, attrs, true); if (DEBUG) { System.out.println("-----> done inflating children"); } if (root != null && attachToRoot) { //如果Root不为null且是attachToRoot==true,则添加创建出来的View到root中,同时把params设置上去 root.addView(temp, params); } if (root == null || !attachToRoot) { result = temp; } } } catch (XmlPullParserException e) { final InflateException ie = new InflateException(e.getMessage(), e); ie.setStackTrace(EMPTY_STACK_TRACE); throw ie; } catch (Exception e) { final InflateException ie = new InflateException( getParserStateDescription(inflaterContext, attrs) + ": " + e.getMessage(), e); ie.setStackTrace(EMPTY_STACK_TRACE); throw ie; } finally { // Don't retain static reference on context. mConstructorArgs[0] = lastContext; mConstructorArgs[1] = null; Trace.traceEnd(Trace.TRACE_TAG_VIEW); } return result; }}
发表评论
最新留言
很好
[***.229.124.182]2025年04月26日 19时04分31秒
关于作者

喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
接收get或post数据使用fwrite写入文件中,方便追踪错误;或其他几种缓存方式
2021-05-10
mysql开启慢查询日志及查询
2021-05-10
Window平台Grpc框架搭建
2021-05-10
C中几道位运算的例题
2021-05-10
python入门(二)基础知识
2021-05-10
golang log4go 使用说明及丢失日志原因
2021-05-10
Android Studio打包生成Jar包的方法
2021-05-10
Excel 如何根据单元格中的值设立不同的颜色(或渐变)?(222)
2021-05-10
python 文件操作 open()与with open() as的区别(打开文件)
2021-05-10
python中列表 元组 字典 集合的区别
2021-05-10
python struct 官方文档
2021-05-10
Docker镜像加速
2021-05-10
静态数组类的封装(泛型)
2021-05-10
操作记录-2021-03-15: sunxiaoyu_project
2021-05-10
Android DEX加固方案与原理
2021-05-10
Android Retrofit2.0 上传单张图片和多张图片
2021-05-10