When writing an Android program, sometimes a custom view is written, using the Inflater object to parse the layout file into a view. The main purpose of this paper is to summarize the methods of obtaining Layoutinflater objects.
1, if you can get the context object, there are several methods:
Layoutinflater Inflater = (layoutinflater) context.getsystemservice (Context.layout_inflater_service); View child = inflater.inflate (R.layout.child, NULL);
Or
Layoutinflater Inflater = layoutinflater.from (context); View child = inflater.inflate (R.layout.child, NULL);
</pre><p></p><pre>
2, in an activity, you can have the following methods:
View child = Getlayoutinflater (). Inflate (R.layout.child, item, FALSE);
Or
View view; Layoutinflater Inflater = (layoutinflater) getcontext (). Getsystemservice (Context.layout_inflater_service); View = Inflater.inflate (r.layout.mylayout, NULL);
Both Method 1 and Method 2 are actually used for the context (). Getsystemservice ().
3. Static method of using view:
View view=view.inflate (context, r.layout.child, NULL)
Inflate realize the source code as follows:
/** * Inflate a view from an XML resource. This convenience method wraps the {@link * Layoutinflater} class, which provides a full range of options for view INFL ation. * * @param context the context object for your activity or application. * @param resource The resource ID to inflate * @param root A View group that would be the parent. Used to properly inflate the * layout_* parameters. * @see layoutinflater * /public static View inflate (context context, int resource, ViewGroup root) { layou Tinflater factory = Layoutinflater.from (context); Return Factory.inflate (resource, root); }
Layoutinflater.from (context) is actually the packaging of Method 1, can refer to the following source code:
/** * Obtains the layoutinflater from the given context. */public static Layoutinflater from (context context) { Layoutinflater layoutinflater = (layoutinflater) Context.getsystemservice (context.layout_inflater_service); if (Layoutinflater = = null) { throw new Assertionerror ("Layoutinflater not Found.") ; return layoutinflater; }