Usage and Differences of Android inflater

  • 2021-10-25 07:49:25
  • OfStack

The LayoutInflater class is still very useful in real-world development, and it works like findViewById (). The difference is that LayoutInflater is used to find the xml layout file under res/layout/and instantiate it; While findViewById () is to find the specific widget control (such as Button, TextView, etc.) under the xml layout file. Specific role:

1. For an interface that is not loaded or wants to be loaded dynamically, it is necessary to use LayoutInflater. inflate () to load;
2. For a loaded interface, you can use the Activiyt. findViewById () method to get the interface elements.

Declaration in the document:


public abstract class LayoutInflater extends Object

Three instantiation methods:

1. LayoutInflater inflater = getLayoutInflater (); //Call getLayoutInflater () of Activity
2.LayoutInflater localinflater =(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
3. LayoutInflater inflater = LayoutInflater.from(context);

In fact, these three ways are essentially the same, as can be seen from the source code:

getLayoutInflater ():

Activity getLayoutInflater () method is to call PhoneWindow getLayoutInflater () method, see 1 under the source code:


 public PhoneWindow(Context context) { 
    super(context); 
    mLayoutInflater = LayoutInflater.from(context); 
} 

You can see that it is actually calling LayoutInflater. from (context).

LayoutInflater. from (context):


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;  
} 

You can see that it actually calls context. getSystemService ().

Conclusion: So the ultimate essence of these three methods is that they are all called Context. getSystemService ().

inflate method

From the api document of sdk, we can know that this method has the following overload forms, and the return values are all View objects, as follows


 public View inflate (int resource, ViewGroup root)
public View inflate (XmlPullParser parser, ViewGroup root)
public View inflate (XmlPullParser parser, ViewGroup root, boolean attachToRoot)
public View inflate (int resource, ViewGroup root, boolean attachToRoot)

Schematic code:


LayoutInflater inflater = (LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE); 
View view = inflater.inflate(R.layout.custom, (ViewGroup)findViewById(R.id.test)); 
//EditText editText = (EditText)findViewById(R.id.content);// error 
EditText editText = (EditText)view.findViewById(R.id.content); 
** For the above code, the 2 Parameters  ViewGroup root Of course, you can also set it to  null  Value. **

Note:

The inflate method is different from the findViewById method.

inflater is used to find xml layout file under res/layout and instantiate it;

findViewById () is to find specific widget controls (such as Button, TextView, etc.) in specific xml layout files.

Summarize


Related articles: