final AttributeSet attrs = Xml.asAttributeSet(parser); Context lastContext = (Context)mConstructorArgs[0]; mConstructorArgs[0] = mContext; 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) { thrownew 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) { thrownew InflateException("<merge /> can be used only with a valid " + "ViewGroup root and attachToRoot=true"); } //递归inflate方法调用 rInflate(parser, root, attrs, false); } else { // Temp is the root view that was found in the xml View temp; if (TAG_1995.equals(name)) { temp = new BlinkLayout(mContext, attrs); } else { temp = createViewFromTag(root, name, 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); //如果attachToRoot=false就调用view的setLayoutParams方法 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 rInflate(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. //root非空且attachToRoot=true则将xml文件的temp 加到形参提供的root里 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. //如果root为null,或者不需要attach到root,返回xml里解析的root view if (root == null || !attachToRoot) { result = temp; } }
} catch (XmlPullParserException e) { InflateException ex = new InflateException(e.getMessage()); ex.initCause(e); throw ex; } catch (IOException 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; }