当前位置: 首页 > news >正文

对于LayoutInflater.from(this).inflate()方法的理解

前言

对于LayoutInflater.from(this).inflate()方法的几个参数以及用法总是迷迷糊糊,源码看了忘,忘了看,因此决定写这篇博客做下记录。

源码解析

我们知道,调用LayoutInflater.from(this).inflate()方法最终都会走三参的方法
public View inflate(@LayoutRes int resource, @Nullable ViewGroup root, boolean attachToRoot)
,这里先简单说明下三个参数:

resource:表示需要加载资源的id;

root:表示resource资源需要被添加的根布局;

attachToRoot:表示resource是否需要被绑定到root上;

接下来我们重点分析下相关源码

inflate(@LayoutRes int resource, @Nullable ViewGroup root, boolean attachToRoot)

    public View inflate(@LayoutRes int resource, @Nullable ViewGroup root, boolean attachToRoot) {
        final Resources res = getContext().getResources();
		...
        View view = tryInflatePrecompiled(resource, res, root, attachToRoot);
        if (view != null) {
            return view;
        }
        XmlResourceParser parser = res.getLayout(resource);
        try {
            return inflate(parser, root, attachToRoot);//重点看这个代码
        } finally {
            parser.close();
        }
    }

inflate(parser, root, attachToRoot)

  public View inflate(XmlPullParser parser, @Nullable ViewGroup root, boolean attachToRoot) {
        synchronized (mConstructorArgs) {
          	...
            View result = root; //result默认赋值为root

            try {
             	...
                if (TAG_MERGE.equals(name)) {
  					.... //merge标签 这里不关注
                } else {
                    // 将temp赋值为xml资源的根布局view
                    final View temp = createViewFromTag(root, name, inflaterContext, attrs);
                    ViewGroup.LayoutParams params = null;
					
                    if (root != null) {
                       	//如果传入的root参数不为空,则创建root对应的LayoutParams
                        params = root.generateLayoutParams(attrs);
                        if (!attachToRoot) {
                            //如果传入的attachToRoot为false,则给temp布局设置LayoutParams
                            temp.setLayoutParams(params);
                        }
                    }

      
                    // 绑定children
                    rInflateChildren(parser, temp, attrs, true);
					...
                    if (root != null && attachToRoot) {
                    	//如果root不为null并且attachToRoot为true时,将temp添加到root中,并设置对应的LayoutParams
                        root.addView(temp, params);
                    }

                    if (root == null || !attachToRoot) {
                    	// root为null 或者 attachToRoot为false时,result = temp
                        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; //返回结果
        }
    }

总结

  • 调用LayoutInflater.from(this).inflate(resource,null)不会将resource添加到布局中,结果返回resource资源根布局view,但不会设置对应的父布局LayoutParams属性【resource布局文件最外层的layout_width和layout_height等跟父布局有关的位置属性都会失效】;

  • 调用LayoutInflater.from(this).inflate(resource,root)会将resource添加到root布局中,同时会设置对应的父布局LayoutParams属性,结果返回root本身;

  • 调用LayoutInflater.from(this).inflate(resource,root,true)LayoutInflater.from(this).inflate(resource,root)方法效果一致,结果返回root本身;

  • 调用LayoutInflater.from(this).inflate(resource,root,false)不会将resource添加到root中,结果返回resource资源根布局view,同时会设置对应的父布局LayoutParams属性

  • 下面代码效果相同;

LayoutInflater.from(this).inflate(resource,root)
等同于
LayoutInflater.from(this).inflate(resource,root,true)
等同于
View resourceView = LayoutInflater.from(this).inflate(resource,root,false)
root.addView(resourceView)

结语

如果以上文章对您有一点点帮助,希望您不要吝啬的点个赞加个关注,您每一次小小的举动都是我坚持写作的不懈动力!ღ( ´・ᴗ・` )

相关文章:

  • koa 使用
  • promise执行顺序面试题令我头秃,你能作对几道
  • <Linux进程通信之共享内存>——《Linux》
  • Go项目实战:02-微服务micro services
  • 清越科技将开启申购:预计募资约8亿元,高裕弟为实际控制人
  • 【Linux】shell 及权限理解
  • 【C语言刷题】PTA基础编程题目集精选
  • CSS实现文字扫光特效
  • vue前端案例教学:动态获取最新疫情数据展示(代码详解)
  • Part9:RPA核心技术之抓屏技术
  • 关于缓存与数据双写一致性问题(清晰易懂)
  • 2022 年时间序列分析最顶流的 Python 库
  • Typora入门教程
  • CSC7720
  • 编译Nginx源码(windows10及centos7)
  • Elasticsearch入门、API操作
  • Linux--进程控制
  • Python——字典
  • 机器学习必会面试知识点
  • C#基于ASP.NET的人事薪资管理系统
  • 新华保险一季度净赚58.82亿增19%,保费收入增28%
  • 中国建设银行浙江省分行原党委书记、行长高强接受审查调查
  • 日本希望再次租借大熊猫,外交部:双方就相关合作保持密切沟通
  • 发出“美利坚名存实亡”呼号的卡尼,将带领加拿大走向何方?
  • 日趋活跃!2024年我国数据生产总量同比增长25%
  • 国家统计局:一季度全国规模以上文化及相关产业企业营业收入增长6.2%