What is LayoutInflater and layoutinflater?

Source: Internet
Author: User

What is LayoutInflater and layoutinflater?
Purpose:
1. Use inflate to load an interface that is not loaded or needs to be dynamically loaded.

2. For a loaded Activity, you can use the findViewById method that implements Activiyt to obtain the interface elements.

Method:
  When you want to create a screen in Android, you generally create a new class for beginners, inherit the Activity base class, and then use the setContentView method in onCreate to load an interface defined in xml.

  In fact, LayoutInflater is used in the Activity to load the interface.GetSystemService (Context. LAYOUT_INFLATER_SERVICE)You can obtain a LayoutInflater by usingLayoutInflater inflater = getLayoutInflater ();And then use the inflate method to load the xml of layout,
The following is a simple example:
First, we need to know what is the loaded layout and what is not yet loaded. we start an application. layout related to the entry Activity {main is common. xml} is loaded, that is, in Oncreate. other layout operations are not loaded. it needs to be dynamically loaded or passed through another activity.
In actual development, the LayoutInflater class is very useful. Its function is similar to findViewById (). The difference is that LayoutInflater is used to find and instantiate xml layout files in layout! FindViewById () is used to find the widget control under the specific xml. in order to make it easy for everyone to understand, I have made a simple Demo for the main layout. xml contains a TextView and a Button. When a Button is clicked, a Dialog is displayed. The layout of this Dialog is the custom_dialog.xml file defined in the layout directory (which is distributed left and right, ImageView on the left, textView on the right ).

The Code is as follows: package com. bivin;
Import android. app. activity; import android. app. alertDialog; import android. content. context; import android. OS. bundle; import android. view. layoutInflater; import android. view. view; import android. view. view. onClickListener; import android. widget. button; import android. widget. imageView; import android. widget. textView;
Public class MainActivity extends Activity implements OnClickListener {
Private Button button;
Public void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. main );
Button = (Button) findViewById (R. id. button); button. setOnClickListener (this );}
@ Overridepublic void onClick (View v ){
ShowCustomDialog ();}
Public void showCustomDialog () {AlertDialog. Builder builder; AlertDialog alertDialog; Context mContext = MainActivity. this;
LayoutInflater inflater = (LayoutInflater) mContext. getSystemService (LAYOUT_INFLATER_SERVICE); View layout = inflater. inflate (R. layout. custom_dialog, null); TextView text = (TextView) layout. findViewById (R. id. text); text. setText ("Hello, Welcome to Mr Wei's blog! "); ImageView image = (ImageView) layout. findViewById (R. id. image); image. setImageResource (R. drawable. icon); builder = new AlertDialog. builder (mContext); builder. setView (layout); alertDialog = builder. create (); alertDialog. show ();}}
How to display the layout introduced by android layoutinflater

SetContentView (view );
 
How does android initialize the value of the text box in the View of LayoutInflater?

Settext ("xxx ");

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.