Android Inflate ()

Source: Internet
Author: User

 

The inflate () function is to find out a layout of the XML definition, but only to find out and hide, not find the same time and display the function. One of the most recent projects I've been doing is that I've been confused for days.

There is also a method called Findviewbyid () similar to inflate () on Android, which can sometimes be used, but there are also differences

The difference is:

If you use other layout in your activity, such as dialog layout, and you want to set the contents of other components on this layout, you must use the inflate () method to find out the layout of the dialog box first, and then use the Findviewbyid () Find the other components above it. 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");
[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");
Note: R.ID.DIALOG_TV is a component on the layout of the dialog box, and if the direct use of This.findviewbyid (R.ID.DIALOG_TV) will definitely be an error.

[HTML]View Plaincopyprint?
    1. View viewstub = ((viewstub) Findviewbyid (R.id.stubview)). Inflate ();
[HTML]View PlainCopyprint?
    1. View viewstub = ((viewstub) Findviewbyid (R.id.stubview)). Inflate ();

Inflate () or can be understood as "hidden expansion", hidden in view, inflate () just get control, but no size does not occupy space in the view, inflate () after a certain size, just out of the hidden state.

The use of layoutinflater and inflate

Here is an example of the tabhost 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 tabhost = this.gettabhost ();
  11. /** 
  12. * Layoutinflater This class acts like Findviewbyid (),
  13. * Different points:
  14. * The Layoutinflater is used to find the layout of the XML layouts file, and it will instantiate
  15. * Findviewbyid () is to find specific widget controls under a specific XML layout file, such as: Button buttons
  16. *
  17. *
  18. *
  19. * Inflate is equivalent to locating a layout defined in an XML.
  20. * Because if you use the Findviewbyid () method directly in an activity file,
  21. * Then it corresponds to the component in the layout that was called in Setconentview ().
  22. * So if you use other layout in the same activity,
  23. * and you have to set the contents of this layout component (e.g. Imageview,textview),
  24. * Then you have to use inflate () to find this layout first, and then use this layout object to find the components on it
  25. * Then perform a series of operations
  26. *
  27. * Parameters in Inflate () method:
  28. * 1. The ID of the layout file you want to use
  29. * 2. Hold the contents of the tab to get Framelayout
  30. * 3.true: The XML file parsed here is the root view
  31. */
  32. Layoutinflater.from (this). Inflate (R.layout.tabhost_layout,
  33. Tabhost.gettabcontentview (), true);
  34. /** is added here:
  35. * 1. You must specify the tab content, which must be the ID, i.e.: setcontent (R.id.text)
  36. * 2. You must set the text or picture on the tab, that is: Setindicator ("answered phone")
  37. * 3. Returns a Tabhost.tabspec object whose parameters are used to identify a tab tag, i.e.: Newtabspec ("Tab1")
  38. */
  39. Tabhost.addtab (Tabhost.newtabspec ("Tab1"). Setindicator ("answered phone")
  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 Call")
  45. . SetContent (R.id.text));
  46. }
  47. }

First, Layoutinflater

Layoutinflater actually finds the XML layout file under res/layout/and instantiates it, which is somewhat similar to Findviewbyid (), which is to find the specific widget controls under the XML layout file (such as button, TextView, etc.)

Role:

1, for a non-loaded or want to dynamically load the interface, you need to use Layoutinflater.inflate () to load;

2, for an already loaded interface, you can use the Activiyt.findviewbyid () method to obtain the interface elements.

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

Public abstract class Layoutinflater extends Object

Three ways to get Layoutinflater instances

1. Layoutinflater inflater = Getlayoutinflater (); Call activity's Getlayoutinflater ()

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);

Second, inflate

In layman's words, inflate is the equivalent of locating a layout defined in an XML. Because if you are using Findviewbyid () directly in an activity, the component in the layout of Setconentview () is the corresponding one.

So if you use other layout in your activity, such as the layout on the dialog, you also have to set the contents of the Layout on the dialog box (like picture ImageView, Text TextView), you have to use inflate () Find the layout of the dialog box first, and then use the layout object to find the components above it, such as:

  

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 on the dialog box, and you use This.findviewbyid directly (R.ID.DIALOG_TV), you will definitely get an error.

Android Inflate ()

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.