/**
* Inflate a new view hierarchy from the specified xml resource. Throws
* {@linkInflateException} if there is an error.
*
* @paramresourceID for an XML layout resource to load (e.g.,
*<code>R.layout.main_page</code>)
* @paramrootOptional view to be the parent of the generated hierarchy (if
*<em>attachToRoot</em> is true), or else simply an object that
* provides a set of LayoutParams values for root of the returned
* hierarchy (if<em>attachToRoot</em> is false.)
* @paramattachToRootWhether 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.
*/publicViewinflate(@LayoutResintresource,@NullableViewGrouproot,booleanattachToRoot){finalResourcesres=getContext().getResources();if(DEBUG){Log.d(TAG,"INFLATING from resource: \""+res.getResourceName(resource)+"\" ("+Integer.toHexString(resource)+")");}Viewview=tryInflatePrecompiled(resource,res,root,attachToRoot);if(view!=null){returnview;}XmlResourceParserparser=res.getLayout(resource);try{returninflate(parser,root,attachToRoot);}finally{parser.close();}}
if(TAG_MERGE.equals(name)){if(root==null||!attachToRoot){thrownewInflateException("<merge /> can be used only with a valid "+"ViewGroup root and attachToRoot=true");}rInflate(parser,root,inflaterContext,attrs,false);}else{...}
...elseif(TAG_INCLUDE.equals(name)){if(parser.getDepth()==0){thrownewInflateException("<include /> cannot be the root element");}parseInclude(parser,context,parent,attrs);}...
...finalViewprecompiled=tryInflatePrecompiled(layout,context.getResources(),(ViewGroup)parent,/*attachToRoot=*/true);if(precompiled==null){finalXmlResourceParserchildParser=context.getResources().getLayout(layout);try{finalAttributeSetchildAttrs=Xml.asAttributeSet(childParser);while((type=childParser.next())!=XmlPullParser.START_TAG&&type!=XmlPullParser.END_DOCUMENT){// Empty.}if(type!=XmlPullParser.START_TAG){thrownewInflateException(getParserStateDescription(context,childAttrs)+": No start tag found!");}finalStringchildName=childParser.getName();if(TAG_MERGE.equals(childName)){// The <merge> tag doesn't support android:theme, so// nothing special to do here.rInflate(childParser,parent,context,childAttrs,false);}else{finalViewview=createViewFromTag(parent,childName,context,childAttrs,hasThemeOverride);finalViewGroupgroup=(ViewGroup)parent;finalTypedArraya=context.obtainStyledAttributes(attrs,R.styleable.Include);finalintid=a.getResourceId(R.styleable.Include_id,View.NO_ID);finalintvisibility=a.getInt(R.styleable.Include_visibility,-1);a.recycle();// We try to load the layout params set in the <include /> tag.// If the parent can't generate layout params (ex. missing width// or height for the framework ViewGroups, though this is not// necessarily true of all ViewGroups) then we expect it to throw// a runtime exception.// We catch this exception and set localParams accordingly: true// means we successfully loaded layout params from the <include>// tag, false means we need to rely on the included layout params.ViewGroup.LayoutParamsparams=null;try{params=group.generateLayoutParams(attrs);}catch(RuntimeExceptione){// Ignore, just fail over to child attrs.}if(params==null){params=group.generateLayoutParams(childAttrs);}view.setLayoutParams(params);// Inflate all children.rInflateChildren(childParser,view,childAttrs,true);if(id!=View.NO_ID){view.setId(id);}switch(visibility){case0:view.setVisibility(View.VISIBLE);break;case1:view.setVisibility(View.INVISIBLE);break;case2:view.setVisibility(View.GONE);break;}group.addView(view);}}finally{childParser.close();}}...
if(TAG_MERGE.equals(childName)){// The <merge> tag doesn't support android:theme, so// nothing special to do here.rInflate(childParser,parent,context,childAttrs,false);}else{...}
// else分支finalViewview=createViewFromTag(parent,childName,context,childAttrs,hasThemeOverride);finalViewGroupgroup=(ViewGroup)parent;finalTypedArraya=context.obtainStyledAttributes(attrs,R.styleable.Include);finalintid=a.getResourceId(R.styleable.Include_id,View.NO_ID);finalintvisibility=a.getInt(R.styleable.Include_visibility,-1);a.recycle();// We try to load the layout params set in the <include /> tag.// If the parent can't generate layout params (ex. missing width// or height for the framework ViewGroups, though this is not// necessarily true of all ViewGroups) then we expect it to throw// a runtime exception.// We catch this exception and set localParams accordingly: true// means we successfully loaded layout params from the <include>// tag, false means we need to rely on the included layout params.ViewGroup.LayoutParamsparams=null;try{params=group.generateLayoutParams(attrs);}catch(RuntimeExceptione){// Ignore, just fail over to child attrs.}if(params==null){params=group.generateLayoutParams(childAttrs);}view.setLayoutParams(params);// Inflate all children.rInflateChildren(childParser,view,childAttrs,true);if(id!=View.NO_ID){view.setId(id);}switch(visibility){case0:view.setVisibility(View.VISIBLE);break;case1:view.setVisibility(View.INVISIBLE);break;case2:view.setVisibility(View.GONE);break;}group.addView(view);
// We try to load the layout params set in the <include /> tag.// If the parent can't generate layout params (ex. missing width// or height for the framework ViewGroups, though this is not// necessarily true of all ViewGroups) then we expect it to throw// a runtime exception.// We catch this exception and set localParams accordingly: true// means we successfully loaded layout params from the <include>// tag, false means we need to rely on the included layout params.ViewGroup.LayoutParamsparams=null;try{params=group.generateLayoutParams(attrs);}catch(RuntimeExceptione){// Ignore, just fail over to child attrs.}if(params==null){params=group.generateLayoutParams(childAttrs);}view.setLayoutParams(params);
/**
* Returns a new set of layout parameters based on the supplied attributes set.
*
* @paramattrsthe attributes to build the layout parameters from
*
* @return an instance of {@linkandroid.view.ViewGroup.LayoutParams} or one
* of its descendants
*/publicLayoutParamsgenerateLayoutParams(AttributeSetattrs){returnnewLayoutParams(getContext(),attrs);}