安卓关于inflate方法的总结

android inflate


前言

android中我们经常用到inflate方法,今天对这个方法进行一个总结,inflate方法调用方式如下:

  • View.inflate(Context context, @LayoutRes int resource, ViewGroup root);
  • LayoutInflater.from(Context context).inflate(@LayoutRes int resource, ViewGroup root, boolean attachToRoot);

说白了,一种是通过View类调用其静态方法inflate。另一种是获取LayoutInflater对象,调用LayoutInflater类的inflate方法。

分析

那我们就分别看下这两个方法,首先看下View类的inflate方法。

代码很简单,通过LayoutInflater.from(context);生成一个LayoutInflater对象,调用其inflate方法,传递参数resource,root.

那我们接着看下LayoutInflater中的inflate方法。LayoutInflater中有好几个inflate方法,但是最终调用的都是下边这个方法,所以我们主要分析下边这个方法。

/**
     * Inflate a new view hierarchy from the specified XML node. Throws
     * {@link InflateException} if there is an error.
     * 

* Important   For performance * reasons, view inflation relies heavily on pre-processing of XML files * that is done at build time. Therefore, it is not currently possible to * use LayoutInflater with an XmlPullParser over a plain XML file at runtime. * * @param parser XML dom node containing the description of the view * hierarchy. * @param root Optional view to be the parent of the generated hierarchy (if * attachToRoot is true), or else simply an object that * provides a set of LayoutParams values for root of the returned * hierarchy (if attachToRoot is false.) * @param attachToRoot Whether the inflated hierarchy should be attached to * the root parameter? If false, root is only used to create the * correct subclass of LayoutParams for the root view in the XML. * @return The root View of the inflated hierarchy. If root was supplied and * attachToRoot is true, this is root; otherwise it is the root of * the inflated XML file. */ 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 { // Look for the root node. 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!"); } final String name = parser.getName(); if (DEBUG) { System.out.println("**************************"); System.out.println("Creating root view: " + name); System.out.println("**************************"); } if (TAG_MERGE.equals(name)) { if (root == null || !attachToRoot) { throw new InflateException(" can be used only with a valid " + "ViewGroup root and attachToRoot=true"); } rInflate(parser, root, inflaterContext, attrs, false); } else { // Temp is the root view that was found in the xml 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); } // Create layout params that match root, if supplied params = root.generateLayoutParams(attrs); if (!attachToRoot) { // Set the layout params for temp if we are not // attaching. (If we are, we use addView, below) temp.setLayoutParams(params); } } if (DEBUG) { System.out.println("-----> start inflating children"); } // Inflate all children under temp against its context. rInflateChildren(parser, temp, attrs, true); if (DEBUG) { System.out.println("-----> done inflating children"); } // We are supposed to attach all the views we found (int temp) // to root. Do that now. if (root != null && attachToRoot) { root.addView(temp, params); } // Decide whether to return the root that was passed in or the // top view found in xml. 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(parser.getPositionDescription() + ": " + 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; } }


我们首先看下这几个参数

  • XmlPullParser parser:我们传过来的resourceid被转化成XmlPullParser对象,用于解析布局。
  • ViewGroup root:这个传进的是一个ViewGroup对象,当attachToRoot为true的时候,root就是当前inflate方法返回view的父view,当attachToRoot为false的时候,root就是一个普通的view,用来帮助决定inflate方法返回view的LayoutParams,这块不理解的话,可以看下view的绘制流程,子view的大小是由父view的measurespec和子view的宽高值决定的。
  • boolean attachToRoot,这个就是上边说的,用来决定inflate返回的view跟root是否存在父子布局关系。true表示存在父子关系,系统会将inflate方法返回的view添加到root的子view里。false不添加。

三个参数都明白了,我们在使用inflate方法时,parser参数是保证要传递的,因为我们要新建一个view保证是要有布局支撑的,root可以为null,但是如果为null就代表inflate返回的view没有容器支撑,那么parser布局你写的宽高等数值可能不会起作用,所以必须传一个root容器,attachToRoot当你想创建的view直接为root的子view就设置为true,如果你想创建的view只是需要一个容器,那么就设置attachToRoot为false。

总结

以上基本把LayoutInflater类的inflate方法分析完了。其实View.inflate调用的还是LayoutInflater中的inflate方法,只是attachToRoot的值被默认设置为false,那么说到头来用哪个方法都是一样的,看我们传参的需求,选择最方便的方式即可。

参考:https://blog.csdn.net/u012702547/article/details/52628453#reply


你可能感兴趣的:(移动开发)