Inflate method usage in Android

Source: Internet
Author: User

 

Inflate () is used to find a layout defined in XML, but it is only found and hidden, and the display function is not found at the same time. This is a recent project that has made me confused for several days.

On Android, a method similar to inflate () is called findviewbyid (), which can be used sometimes, but is also different.

The difference is:

If you use another layout in your activity, such as the dialog box layout, you need to set the content of other components on this layout, you must use inflate () the method first finds out the layout of the dialog box, and then finds other components above it with findviewbyid. For example:

ViewView1=View. Inflate (this, R. layout. dialog_layout, null );
    1. Textviewdialogtv= (Textview) view1.findviewbyid (R. Id. dialog_ TV );
    2. Dialogtv. settext ("ABCD ");

Note: R. Id. dialog_ TV is a component in the layout dialog box.This. findviewbyid (R. Id. dialog_ TV) will certainly report an error.


ViewViewstub= (Viewstub) findviewbyid (R. Id. stubview). Inflate ();

inflate () it can be understood as "implicit property expansion ", hidden display in the view. Before inflate (), only the control is obtained, but no size does not occupy space in the view. Inflate () has a certain size, only out of the hidden state

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.