Android LayoutInflater 源码分析及个人总结

来源:互联网 发布:淘宝怎样刷好评 编辑:程序博客网 时间:2024/06/06 17:06

我们经常用到的布局解析器LayoutInflater,主要在ListView或者RecycleView的Adapter中,用两个常用方法

第一个方法是 LayoutInflater.from(context).inflate(resource, root);

第二个方法是LayoutInflater.from(context).inflate(resource, root, attachToRoot);

第一个参数resource是布局,第二个root是父布局,第三个参数是决定是否把执行root.add(resource);

这两个方法用一下四种用法:

LayoutInflater.from(context).inflate(R.layout.main_activity, null);LayoutInflater.from(context).inflate(R.layout.main_activity, android.R.id.content);LayoutInflater.from(context).inflate(R.layout.main_activity, android.R.id.content, false);LayoutInflater.from(context).inflate(R.layout.main_activity, android.R.id.content, true);


先上LayoutInflater的inflate(XmlPullParser parser, @Nullable ViewGroup root, boolean attachToRoot)方法源码

 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("<merge /> 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) {                InflateException ex = new InflateException(e.getMessage());                ex.initCause(e);                throw ex;            } catch (Exception e) {                InflateException ex = new InflateException(                        parser.getPositionDescription()                                + ": " + e.getMessage());                ex.initCause(e);                throw ex;            } finally {                // Don't retain static reference on context.                mConstructorArgs[0] = lastContext;                mConstructorArgs[1] = null;            }            Trace.traceEnd(Trace.TRACE_TAG_VIEW);            return result;        }    }

由此可见

使用第一种方式是会在infalte中调用View temp = createViewFromTag(root, name, inflaterContext, attrs),但不会temp.setLayoutParams(params),所以temp;
的layoutParams为空,这是解析出来的布局的大小会有一些问题,当你在需要解析的布局中的根布局设置android:layout_width,android:layout_height不会生效;

第二种方式和第四种方式相同,会执行root.addView(temp, params),并返回root;

第三种方式,也会直接返回temp,但是会执行temp.setLayoutParams(params),这时在根布局设置android:layout_width,android:layout_height会生效。






1 0