A summary of methods for acquiring Layoutinflater objects in Android development _android

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.