SoFunction
Updated on 2025-03-03

Introduction to the method in Android

Introduction to the () method in Android

I have been trying to figure out the usage of the inflate method of the LayoutInflater object recently, and I made an example today.

<LinearLayout 
    android: 
    android:layout_width="match_parent" 
    android:layout_height="200dp" 
    android:background="#FF0000" 
    android:orientation="vertical" > 
  </LinearLayout> 
itemGroup = (LinearLayout) findViewById(.ll_item_Group); 

This is used as itemGroup object.

<LinearLayout xmlns:andro 
  android:layout_width="match_parent" 
  android:layout_height="match_parent" 
  android:orientation="vertical" > 
 
  <LinearLayout 
    android: 
    android:layout_width="match_parent" 
    android:layout_height="100dp" 
    android:background="#4169E1" 
    android:orientation="horizontal" > 
  </LinearLayout> 
 
  <RelativeLayout 
    android: 
    android:layout_width="100dp" 
    android:layout_height="match_parent" 
    android:background="#00008B" > 
  </RelativeLayout> 
 
</LinearLayout> 

This is the view referenced as include. The test code is as follows: (inflater is an instance of the LayoutInflater object. The method of obtaining it is: inflater = (this), and the other two methods are owned by Baidu)

View v1 = (.el_include, null); 
View v3 = (.el_include, itemGroup, false); 
     
View v2 = (.el_include, itemGroup); 
View v4 = (.el_include, itemGroup, true); 

The test results are:

1. V1 and V3 display the same effect in the Activity, both the original content of itemGroup, and V1 and V3 are both View objects in .el_include.

2. V2 and V4 display the same effect in Activity, both after itemGroup adds the content in .el_include. V2 and V4 objects are both itemGroup with .el_include.

The same effect of V2 and V4 in the Activity indicates that the itemGroup has not changed!

The same display effect in the Activity of V2 and V4 indicates that the itemGroup has changed. Both the content in .el_include is added to the View after the itemGroup.

Then the difference between merge and include is:

What is referenced by include is an independent View, and the View referenced by merge must be placed in a ViewGroup. As shown in the following example:



<merge xmlns:andro 
  android:layout_width="match_parent" 
  android:layout_height="match_parent" > 
 
  <LinearLayout 
    android: 
    android:layout_width="match_parent" 
    android:layout_height="100dp" 
    android:background="#4169E1" 
    android:orientation="horizontal" > 
  </LinearLayout> 
 
  <RelativeLayout 
    android: 
    android:layout_width="100dp" 
    android:layout_height="match_parent" 
    android:background="#800080" > 
  </RelativeLayout> 
 
</merge> 

The .el_marge reference must be like this:

View v = (.el_marge, itemGroup, true); 

Otherwise, an error will be reported:<merge /> can be used only with a valid ViewGroup root and attachToRoot=true

In other words: merge is to reduce the root ViewGroup in include, so the inflate marge must be placed in the ViewGroup.

There are also constant talk about marge and framelayout on the Internet, but I actually don’t think there is any connection. That is, if .el_marge does not add an element in the ViewGroup, the rules are the same as FrameLayout.

If you have any questions, please leave a message or go to the community of this site to exchange and discuss. Thank you for reading. I hope it can help you. Thank you for your support for this site!