助力软件开发企业降本增效 PHP / java源码系统,只需一次付费,代码终身使用! 广告
[TOC] inflate 一词本意是膨胀、充气,在 LayoutInflater 里我们可以把它理解为加载出来的意思,LayoutInflater 即布局加载器,从指定的 xml 资源文件中加载出具有层次的视图结构。inflate 方法应该是我们日常码代码中最常用的方法之一,今天对 inflate 方法进行分析。 ```java View view = LayoutInflater.from(parent.getContext()).inflate( R.layout.item_timed_list, parent, false); ``` 上面这一行代码大家都很熟悉,就是从布局文件中加载 View 到父容器中。那么几个参数又分别代表了什么呢?我们一一来看: # from()方法 LayoutInflater 的 from 方法,从给定的 Context 获取到 LayoutInflater,这个就是调用系统的 getSystemService 方法: ```java public static LayoutInflater from(Context context) { LayoutInflater LayoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); if (LayoutInflater == null) { throw new AssertionError("LayoutInflater not found."); } return LayoutInflater; } ``` 另外通过 Activity 实例的 getLayoutInflater 方法同样可以获取到 LayoutInflater实例。 # inflate()方法 ## 方法重载 LayoutInflater 的 inflate 方法共有 4 种重载: ```java // 方法A public View inflate(@LayoutRes int resource, @Nullable ViewGroup root) { return inflate(resource, root, root != null); } // 方法B public View inflate(XmlPullParser parser, @Nullable ViewGroup root) { return inflate(parser, root, root != null); } // 方法C public View inflate(@LayoutRes int resource, @Nullable ViewGroup root, boolean attachToRoot) { ... } // 方法D public View inflate(XmlPullParser parser, @Nullable ViewGroup root, boolean attachToRoot) { ... } ``` 方法A和方法B很简单,分别调用了方法C和方法D。方法C和方法D的区别在于第一个参数,方法C接收的为布局ID,方法D接收的为布局资源解释器XmlResourceParser。 ```java public View inflate(@LayoutRes int resource, @Nullable ViewGroup root, boolean attachToRoot) { //... final XmlResourceParser parser = res.getLayout(resource); //... return inflate(parser, root, attachToRoot); } ``` 其实方法C拿到布局ID后也是生成XmlResourceParser再调用方法D。 ## 源码分析 虽然方法C最终调用的是方法D,但我们调用inflate方法时一般调用方法C,很少直接调用方法D的。 先来看看方法C几个参数的含义: * resource:想要加载的 xml 布局资源文件的 ID * root:当 attachToRoot 为 true 时,root 即为 inflate 方法加载出来的 View 的根布局;不然 root 仅仅为将要加载出来的 View 提供一组 LayoutParams 参数而已 * attachToRoot:这个值指即将加载出来的 View 是否会附加到上面的 root 中 * 返回值:inflate 方法的返回值是一个 View,指即将加载出来 视图结构的根 View。如果上面提供了 root,并且 attachToRoot 为真时,返回的就是 root;否则为即将加载出来视图结构的根 View 看下方法C的源码: ```java public View inflate(@LayoutRes int resource, @Nullable ViewGroup root, boolean attachToRoot) { ... // 根据布局资源文件得到一个 XmlResourceParser final XmlResourceParser parser = res.getLayout(resource); try { return inflate(parser, root, attachToRoot); } finally { parser.close(); } } ``` 看到调用了 inflate(XmlPullParser parser, ViewGroup root, boolean attachToRoot)方法,接下来我们只看看关键的代码: ```java public View inflate(XmlPullParser parser, @Nullable ViewGroup root, boolean attachToRoot) { final AttributeSet attrs = Xml.asAttributeSet(parser); View result = root; // 创建View final View temp = createViewFromTag(root, name, inflaterContext, attrs); ViewGroup.LayoutParams params = null; // 1、root不为空时 if (root != null) { // 根据xml配置的属性,生成View的LayoutParams params = root.generateLayoutParams(attrs); // 创建出的View不附加到root时 if (!attachToRoot) { // 1.1、把刚生成的LayoutParams设置给View(一会儿View直接就是返回值了) temp.setLayoutParams(params); } } // 递归加载所有子 View // rInflateChildren(parser, temp, attrs, true); // 1.2、root不为空,且attachToRoot为true时,把View添加到root中 if (root != null && attachToRoot) { root.addView(temp, params); } // 2、root为空 if (root == null || !attachToRoot) { result = temp; } return result; } ``` ## 整体总结 可以看到: 1、root不为空时,根据xml中配置的属性,生成LayoutParams。 * 1.1、加载出的View不附加到root时,直接给View设置LayoutParams,root就没事了 * 1.2、加载出的View附加到root时,把View添加到root中,使用该LayoutParams 2、root为空时,View直接创建出来就没了,就没设置LayoutParams那一步了 3、返回值问题 * 3.1、root为空或root不空但不附加到root时,直接返回创建出来的View * 3.2、root不空且附加到root时,返回的是root(当然View已经附加到里面了) > 备注 现在回头来看看构造方法C中的三个参数的作用: 1、resource制定了资源文件的ID,必不可少 2、root仅仅是为了给View生成LayoutParams,其他没啥用了 3、attachToRoot决定了要不要把View附加到root中