/** * Obtains the LayoutInflater from the given context. */publicstaticLayoutInflaterfrom(Contextcontext){LayoutInflaterLayoutInflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);if(LayoutInflater==null){thrownewAssertionError("LayoutInflater not found.");}returnLayoutInflater;}
/** * Inflate a new view hierarchy from the specified xml resource. Throws * {@link InflateException} if there is an error. * * @param resource ID for an XML layout resource to load (e.g., * <code>R.layout.main_page</code>) * @param root Optional view to be the parent of the generated hierarchy. * @return The root View of the inflated hierarchy. If root was supplied, * this is the root View; otherwise it is the root of the inflated * XML file. */publicViewinflate(intresource,ViewGrouproot){returninflate(resource,root,root!=null);}
/** * Inflate a new view hierarchy from the specified xml resource. Throws * {@link InflateException} if there is an error. * * @param resource ID for an XML layout resource to load (e.g., * <code>R.layout.main_page</code>) * @param root Optional 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.) * @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. */publicViewinflate(intresource,ViewGrouproot,booleanattachToRoot){finalResourcesres=getContext().getResources();if(DEBUG){Log.d(TAG,"INFLATING from resource: \""+res.getResourceName(resource)+"\" ("+Integer.toHexString(resource)+")");}finalXmlResourceParserparser=res.getLayout(resource);try{returninflate(parser,root,attachToRoot);}finally{parser.close();}}
/** * Inflate a view from an XML resource. This convenience method wraps the {@link * LayoutInflater} class, which provides a full range of options for view inflation. * * @param context The Context object for your activity or application. * @param resource The resource ID to inflate * @param root A view group that will be the parent. Used to properly inflate the * layout_* parameters. * @see LayoutInflater */publicstaticViewinflate(Contextcontext,intresource,ViewGrouproot){LayoutInflaterfactory=LayoutInflater.from(context);returnfactory.inflate(resource,root);}