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