Layoutinflater.inflate () is used more frequently in Android, either dynamically adding a view or creating a ViewHolder in recyclerView. Adapter. I’ll check it later.

Inflate () and its overload have the following:

  1. View inflate(@LayoutRes int resource, @Nullable ViewGroup root)
  2. View inflate(@LayoutRes int resource, @Nullable ViewGroup root, boolean attachToRoot)
  3. View inflate(XmlPullParser parser, @Nullable ViewGroup root)
  4. View inflate(XmlPullParser parser, @Nullable ViewGroup root, boolean attachToRoot)

For the three parameters in method 2:

Resource: id of the layout file

Root: a view of the ViewGruop type. This view is used with attachToRoot

  • When root is nuL, the attachToRoot value of True or false makes no difference. View is returned as the root view in the XML layout file (i.e., the temp variable in the source code), and LayoutParams are null.

In this case, only the view object corresponding to XML is returned. LayoutParams is not set and no other processing is done.

  • When root is not null and attachToRoot is true, root is the parent control of the XML file. The width and height of the view are defined in the XML file, and LayoutParams are not null.

The case is returned as root with the XML added to root via root.addView(temp, params) and the params parameter specified.

  • When root is not null and attachToRoot is false, root is not the parent control of the XML file, the width and height of the view are the width and height defined in the XML file, and LayoutParams are not null.

SetLayoutParams (params) allows other views to be added to the layout by addView(), and the view has its own params parameter.

Method 1 internally calls 2,2 internally calls 4; 3 internally calls 4, focusing on method 4.

LayoutInflater.java public View inflate(XmlPullParser parser, @Nullable ViewGroup root, boolean attachToRoot) { ................ // Temp is the root view that was found in the XML // Obtain the root view Temp from the XML file final View Temp = createViewFromTag(root, name, inflaterContext, attrs); ViewGroup.LayoutParams params = null; if (root ! = null) { if (DEBUG) { System.out.println("Creating params from root: " + root); } // Create layout params that match root, If supplied / / from an XML file to obtain the width and the height, the background color, and other attributes params = root. GenerateLayoutParams (attrs); if (! AttachToRoot) {// If parameter three is false, set params temp.setLayoutParams(params) to temp; }}... // We are supposed to attach all the views We found (int temp) // to root. Do that now. Add temp to root with the layout parameter params if (root! = null && attachToRoot) { root.addView(temp, params); } // Decide whether to return the root that was passed in or the // top view found in XML. Not for its set params, its params null / / root is not null and three parameters to false, return the result to the root of the XML view if (root = = null | |! attachToRoot) { result = temp; }}Copy the code

It is important to note that the params parameter is null.

When we add a view through addView (View), it’s important that the view has params.

ViewGroup.java

public void addView(View child) {
        addView(child, -1);
}
    public void addView(View child, int index) {
        if (child == null) {
            throw new IllegalArgumentException("Cannot add a null child view to a ViewGroup");
        }
        LayoutParams params = child.getLayoutParams();
        if (params == null) {
            params = generateDefaultLayoutParams();
            if (params == null) {
                throw new IllegalArgumentException("generateDefaultLayoutParams() cannot return null");
            }
        }
        addView(child, index, params);
  }
Copy the code

When the child LayoutParams layout parameter is null, through generateDefaultLayoutParams () method to create LayoutParams, different layouts, The realization of its generateDefaultLayoutParams () is not the same.

  • Linear layout: If the layout is horizontal, add layoutparams.wrap_content layout parameters to the view. If its orientation is vertical, a layout parameter of type layoutparams.match_parent with width layoutparams.wrap_content is added to the view.
  • Relative layout: Add layout parameters of type layoutparams.wrap_content to view by default.
  • Frame layout: Add layout parameters of type layoutparams.match_parent to view by default.
ViewGroup.java protected LayoutParams generateDefaultLayoutParams() { return new LayoutParams(LayoutParams.WRAP_CONTENT,  LayoutParams.WRAP_CONTENT); } LinearLayout.java protected LayoutParams generateDefaultLayoutParams() { if (mOrientation == HORIZONTAL) { return new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); } else if (mOrientation == VERTICAL) { return new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT); } return null; } RelativeLayout.java protected ViewGroup.LayoutParams generateDefaultLayoutParams() { return new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); } FrameLayout.java protected LayoutParams generateDefaultLayoutParams() { return new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT); }Copy the code

Because different layouts add Layoutparams with different width and height limits to views with null params in addView, the width and height we specified for the root view in the XML file will not take effect.

  • With parent as a RelativeLayout: The view’s width and height is wrapConent.
  • When parent is FramLayout: The width and height of the view is the width and height of the frame layout.
  • Parent for LinearLayout: direction is horizontal, view width and height is wrapConent; When the direction is vertical, the view width is the LinearLayout width and the height is wrapConent.