Android inflate () method usage

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:

[HTML]
View plaincopyprint?
  1. View view1 = view. Inflate (this, R. layout. dialog_layout, null );
  2. Textviewdialogtv = (textview) view1.findviewbyid (R. Id. dialog_ TV );
  3. Dialogtv. settext ("ABCD ");
View view1=View.inflate(this,R.layout.dialog_layout,null);    TextViewdialogTV=(TextView)view1.findViewById(R.id.dialog_tv);    dialogTV.setText("abcd");

Note: R. Id. dialog_ TV is a component in the layout dialog box. If you use this. findviewbyid (R. Id. dialog_ TV) directly, an error is returned.


[HTML]
View plaincopyprint?
  1. View viewstub = (viewstub) findviewbyid (R. Id. stubview). Inflate ();
View viewStub = ((ViewStub) findViewById(R.id.stubView)).inflate();

Inflate () can be understood as "implicit expansion", which is placed in the view implicitly. Before inflate (), only the control is obtained, but the control has no size and does not occupy space in the view. Inflate () there is a certain size, just out of the hidden state.

 

Layoutinflater and inflate usage

Here we use the tabhost example to illustrate:

[Java]
View plaincopyprint?
  1. Package CN. csdn. activity;
  2. Import Android. App. tabactivity;
  3. Import Android. OS. Bundle;
  4. Import Android. View. layoutinflater;
  5. Import Android. widget. tabhost;
  6. Public class tabhostactivity extends tabactivity {
  7. @ Override
  8. Protected void oncreate (bundle savedinstancestate ){
  9. Super. oncreate (savedinstancestate );
  10. Tabhost = This. gettabhost ();
  11. /**
  12. * Layoutinflater is similar to findviewbyid (),
  13. * Differences:
  14. * Layoutinflater is used to find the XML layout file in layout, and it will be instantiated.
  15. * Findviewbyid () is used to find the widget control in a specific XML layout file, such as the button.
  16. *
  17. *
  18. *
  19. * Inflate is equivalent to finding the layout defined in an XML file.
  20. * If the findviewbyid () method is directly used in an activity file,
  21. * It corresponds to the component in the layout called in setconentview.
  22. * If other layout items are used in the same activity,
  23. * You also need to set the content of the components in layout (such as imageview and textview,
  24. * Then you must use inflate () to first find the layout, and then use the layout object to find the components above it.
  25. * Perform a series of operations
  26. *
  27. * Parameters in the inflate () method:
  28. * 1. ID of the layout file to be used
  29. * 2. Hold the content of the tab and obtain framelayout.
  30. * 3. True: Use the XML file parsed here as the Root View
  31. */
  32. Layoutinflater. From (this). Inflate (R. layout. tabhost_layout,
  33. Tabhost. gettabcontentview (), true );
  34. /** When you add it here:
  35. * 1. You must specify the content of the tab, which must be ID, that is, setcontent (R. Id. text)
  36. * 2. You must set text or images on the tab, that is, setindicator ("connected ")
  37. * 3. Return a tabhost. tabspec object. The parameter is used to identify the tag of a tab, that is, newtabspec ("tab1 ")
  38. */
  39. Tabhost. addtab (tabhost. newtabspec ("tab1"). setindicator ("answered ")
  40. . Setcontent (R. Id. Text ));
  41. Tabhost. addtab (tabhost. newtabspec ("tab2"). setindicator ("outgoing call ",
  42. Getresources (). getdrawable (R. drawable. ic_launcher ))
  43. . Setcontent (R. Id. Text ));
  44. Tabhost. addtab (tabhost. newtabspec ("tab3"). setindicator ("missed ")
  45. . Setcontent (R. Id. Text ));
  46. }
  47. }

 

1. layoutinflater

Layoutinflater finds the XML layout file under Res/layout/and instantiates it. This is similar to findviewbyid, the latter is to find the specific widget controls (such as button and textview) in the XML layout file)

Purpose:

1. layoutinflater. Inflate () is required for loading an interface that is not loaded or that is to be dynamically loaded;

2. For a loaded interface, you can use the activiyt. findviewbyid () method to obtain the interface elements.

Layoutinflater is an abstract class, which is declared as follows in the document:

Public abstract class layoutinflater extends object

Three methods for obtaining a layoutinflater instance

1. layoutinflater Inflater = getlayoutinflater (); // call the getlayoutinflater () of the activity ()

Example: View toastroot = getlayoutinflater (). Inflate (R. layout. Toast, null );

2. layoutinflater localinflater = (layoutinflater) Context. getsystemservice (context. layout_inflater_service );

3. layoutinflater Inflater = layoutinflater. From (context );

Example: View convertview = layoutinflater. From (mcontext). Inflate (R. layout. activity_contact, null );

2. Inflate

In general, inflate is equivalent to finding the layout defined in an XML file. if findviewbyid () is used directly in an activity, it corresponds to the component in the layout of setconentview.

Therefore, if you use another layout in your activity, such as layout in the dialog box, you must set the content of components (such as image imageview and text textview) in layout in the dialog box, you must use inflate () to first find the layout in the dialog box, and then use this layout object to find the components above it, such:

  

View view = view. Inflate (this, R. layout. dialog_layout, null );

Textview dialogtv = (textview) view. findviewbyid (R. Id. dialog_ TV );

  

Dialogtv. settext ("ABCD ");

  

If the component R. Id. dialog_ TV is a component in the dialog box, you can directly use this. findviewbyid (R. Id. dialog_ TV) to report an error.

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.