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:
- view plaincopy to clipboardprint?
- LayoutInflater inflater = LayoutInflater.from(this);
- View view=inflater.inflate(R.layout.ID, null);
- Or simply put it in one sentence:
- View view=LayoutInflater.from(this).inflate(R.layout.ID, null);
Another method:
- view plaincopy to clipboardprint?
- LayoutInflater inflater = (LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);
- 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():
- Java Code
- public static LayoutInflater from(Context context) {
- LayoutInflater LayoutInflater =
- (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
- if (LayoutInflater == null) {
- throw new AssertionError("LayoutInflater not found.");
- }
- return LayoutInflater;
- }
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 |
- Java Code
- //Basic usage
- public void showCustomDialog(){
- AlertDialog.Builder builder;
- AlertDialog alertDialog;
- Context mContext = AppActivity.this;
- //Both of the following methods are acceptable.
- //LayoutInflater inflater = getLayoutInflater();
- 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();
- }
- }
- protected void showToast(int type) {
- Toast.makeText(this, "*********", Toast.LENGTH_LONG).show();
- LayoutInflater li = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
- View view = li.inflate(R.layout.toast, null);
- Toast toast = new Toast(this);
- toast.setView(view);
- toast.setDuration(type);
- toast.show();
- }
Reprinted at: https://www.cnblogs.com/hnrainll/archive/2012/06/12/2545799.html