This article is an example of the Android development approach to acquiring Layoutinflater objects. Share to everyone for your reference, specific as follows:
When you write an Android program, you sometimes write custom view and use the Inflater object to parse the layout file into a view. The main purpose of this paper is to summarize the methods of acquiring Layoutinflater objects.
1, if can obtain the context object, may have the following 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);
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);
Method 1 and Method 2 are actually used for 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 XML resource. This convenience method wraps the {@link
* Layoutinflater} class, which provides a full range of options for view INFL ation.
* * @param context of 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, int resource, ViewGroup root) {
Layout Inflater 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;
}
More interested readers of Android-related content can view this site's topics: "Introduction to Android Development and advanced Tutorials", "Summary of Android Control usage", "Android SMS and phone operation Tips" and "Android Multimedia operating Tips Summary" (audio, video, Recording, etc.) "
I hope this article will help you with the Android program.