The Use of Layout Inflate in android

Keywords: Java xml Android Mobile

Links to the original text: http://www.cnblogs.com/hnrainll/archive/2012/06/12/2545799.html

Inflater means inflation in English, which should mean expansion in Android.  
LayoutInflater works like findViewById(), except that LayoutInflater is used to find the xml layout file under the layout folder and instantiate it! findViewById() is to find a specific widget control under a specific xml (e.g. Button,TextView, etc.).

(0) There are many places she can use, such as the getView of BaseAdapter, the widget of the component in the view in the customized Dialog, and so on.
It is used in two ways:

Copy to Clipboard Java Code
  1. view plaincopy to clipboardprint?  
  2. LayoutInflater inflater = LayoutInflater.from(this);     
  3. View view=inflater.inflate(R.layout.ID, null);    
  4. Or simply put it in one sentence:    
  5. View view=LayoutInflater.from(this).inflate(R.layout.ID, null);    



Another method:

Copy to Clipboard Java Code
  1. view plaincopy to clipboardprint?  
  2. LayoutInflater inflater = (LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);    
  3. View view=inflater.inflate(R.layout.ID, null);    


The two methods above are essentially the same. Look at the source code below, form() calls getSystemService():
Copy to Clipboard Java Code
  1. Java Code  
  2. public static LayoutInflater from(Context context) {       
  3.     LayoutInflater LayoutInflater =       
  4.             (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);       
  5.     if (LayoutInflater == null) {       
  6.         throw new AssertionError("LayoutInflater not found.");       
  7.     }       
  8.     return LayoutInflater;       
  9. }     



In addition, getSystemService() is an important API of Android. It is a method of Activity, which obtains the corresponding Object according to the incoming NAME, and then converts it into the corresponding service object. The corresponding services of the system are described below.  

Input Name Objects returned Explain
WINDOW_SERVICE WindowManager Manage open window programs
LAYOUT_INFLATER_SERVICE LayoutInflater Get the view defined in xml
ACTIVITY_SERVICE ActivityManager Administration application program System state
POWER_SERVICE PowerManger Power Supply Services
ALARM_SERVICE AlarmManager Alarm clock service
NOTIFICATION_SERVICE NotificationManager Status Bar Services
KEYGUARD_SERVICE KeyguardManager Keyboard Lock Service
LOCATION_SERVICE LocationManager Location services, such as GPS
SEARCH_SERVICE SearchManager Search Services
VEBRATOR_SERVICE Vebrator Mobile Vibration Service
CONNECTIVITY_SERVICE Connectivity Network Connection Services
WIFI_SERVICE WifiManager Wi-Fi Service
TELEPHONY_SERVICE TeleponyManager telephony support


Copy to Clipboard Java Code
  1. Java Code  
  2. //Basic usage  
  3. public void showCustomDialog(){    
  4.   AlertDialog.Builder builder;    
  5.   AlertDialog alertDialog;    
  6.   Context mContext = AppActivity.this;    
  7. //Both of the following methods are acceptable.  
  8.   //LayoutInflater inflater = getLayoutInflater();    
  9.   LayoutInflater inflater = (LayoutInflater)     
  10. mContext.getSystemService(LAYOUT_INFLATER_SERVICE);    
  11.   View layout = inflater.inflate(R.layout.custom_dialog,null);    
  12.   TextView text = (TextView) layout.findViewById(R.id.text);    
  13.   text.setText("Hello, Welcome to Mr Wei's blog!");    
  14.   ImageView image = (ImageView) layout.findViewById(R.id.image);    
  15.   image.setImageResource(R.drawable.icon);    
  16.   builder = new AlertDialog.Builder(mContext);    
  17.   builder.setView(layout);    
  18.   alertDialog = builder.create();    
  19.   alertDialog.show();    
  20.  }    
  21. }    
  22.     
  23. protected void showToast(int type) {      
  24.         Toast.makeText(this"*********", Toast.LENGTH_LONG).show();      
  25.       
  26.         LayoutInflater li = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);      
  27.         View view = li.inflate(R.layout.toast, null);      
  28.               
  29.         Toast toast = new Toast(this);      
  30.         toast.setView(view);      
  31.         toast.setDuration(type);      
  32.         toast.show();      
  33.     }      


Reprinted at: https://www.cnblogs.com/hnrainll/archive/2012/06/12/2545799.html

Posted by Bailz on Wed, 24 Jul 2019 00:12:37 -0700