Layoutinflater function and use

Source: Internet
Author: User

function:Used to instantiate an XML file into a specified view. Similar to Findviewbyid, the difference is that the layoutinflater found in the res file is not loaded or needs to be loaded dynamically, for the already loaded activity can use Findviewbyid to obtain the interface elements.
There are several ways to get Layoutinflater:
  
 
  1. 1. LayoutInflater inflater = getLayoutInflater();
  2. //调用Activity的getLayoutInflater() Returns LayoutInflater The shared LayoutInflater.
  3. 2. LayoutInflater inflater = LayoutInflater.from(context);
  4. //Obtains the LayoutInflater from the given context.
  5. 3. LayoutInflater inflater = (LayoutInflater)context.getSystemService
  6. (Context.LAYOUT_INFLATER_SERVICE);
  7. //A LayoutInflater for inflating layout resources in this context
From the source to get three kinds of call Context.getsystemservice (), it according to name to obtain the corresponding object and then converted to the corresponding service object. Http://developer.android.com/reference/android/content/Context.html#getSystemService (java.lang.String) layout_ Inflater_serviceLayoutinflater Gets the view defined in the XML.

back to view:Use the Inflater method.
 
   
  
  1. View view = inflater.inflate(R.layout.newview, null);
  2. setContentView(view);//显示









From for notes (Wiz)

Layoutinflater function and use

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.