Android--inflate to find the layout defined in an XML

Source: Internet
Author: User

In layman's words, inflate is the equivalent of locating a layout defined in an XML.
  
Because if you use 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:
  
  Viewview=view.inflate (This,r.layout.dialog_layout,null);
  
textviewdialogtv= (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.
  
  There are three ways to generate Layoutinflater:
  
  Layoutinflaterinflater=layoutinflater.from (this);
  
Layoutinflaterinflater=getlayoutinflater ();
  
Layoutinflaterinflater= (Layoutinflater) This.getsystemservice (Layout_inflater_service);
  
Then call the inflate method to turn the XML layout file into view

layoutinflater layoutinflater = layoutinflater. from (Dialogactivity.this);
Get a custom dialog box
Final View dialogview = layoutinflater. Inflate (r.layout.dialog_login, NULL);


  
Publicviewinflate (Intresource,viewgrouproot,booleanattachtoroot)
  
In the view class, there are also inflate methods
  
Publicstaticviewinflate (Contextcontext,intresource,viewgrouproot)

Citation: http://android.tgbus.com/Android/tutorial/201104/348009.shtml

Android--inflate to find the layout defined in an XML

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.