Use the include tag to extract reusable components (reference layout)
Use the merge label to reduce the nesting level of the layout (merge equals Framelayout)
Scenario 1: The Layout root node is framelayout and does not need to set properties such as background or padding, which can be replaced with merge.
Scenario 2: When a layout is added as a child layout by another layout, use merge as the top node of the layout, so that when introduced, the top nodes are automatically ignored.
Third, use the viewstub tag to load some of the less common layout
Role: Viewstub tags can be used to introduce an external layout as well as include tags, but the viewstub introduced layouts do not expand by default, neither occupy the display nor occupy positions, thus saving CPU and memory when parsing layout
Example one: Include
The following is the Res/layout/title.xml layout file:
<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"
Xmlns:tools= "Http://schemas.android.com/tools"
Android:layout_width= "Match_parent"
android:layout_height= "Wrap_content"
android:background= "@drawable/TITLE_BG" >
<button
Android:id= "@+id/title_back"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:layout_gravity= "Center"
Android:layout_margin= "5DP"
android:background= "@drawable/BACK_BG"
Android:text= "Back"
Android:textcolor= "#fff"/>
<textview
Android:id= "@+id/title_text"
Android:layout_width= "0DP"
android:layout_height= "Wrap_content"
android:layout_gravity= "Center"
android:layout_weight= "1"
android:gravity= "Center"
Android:text= "This is Title"
Android:textcolor= "#fff"
Android:textsize= "25SP"/>
<button
Android:id= "@+id/title_edit"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:layout_gravity= "Center"
Android:layout_margin= "5DP"
android:background= "@drawable/EDIT_BG"
android:text= "Edit"
Android:textcolor= "#fff"/>
The following is the Res/layout/activity_main.xml layout file:
<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"
Xmlns:tools= "Http://schemas.android.com/tools"
Android:layout_width= "Match_parent"
android:layout_height= "Wrap_content" >
Example two: Merge
The following is the Res/layout/progress.xml layout file:
-->
<merge xmlns:android= "Http://schemas.android.com/apk/res/android"
Android:layout_width= "Match_parent"
android:layout_height= "Match_parent"
android:orientation= "Vertical" >
<progressbar
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:layout_gravity= "Center"/>
The following is the Res/layout/activity_main.xml layout file:
<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"
Xmlns:tools= "Http://schemas.android.com/tools"
Android:layout_width= "Match_parent"
android:layout_height= "Wrap_content"
android:orientation= "Vertical" >
<framelayout
Android:layout_width= "Match_parent"
android:layout_height= "Wrap_content" >
<textview
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:text= "Body Content"
Android:textsize= "25SP"/>
Example three: Viewstub
The following is the Res/layout/activity_main.xml layout file:
<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"
Xmlns:tools= "Http://schemas.android.com/tools"
Android:layout_width= "Match_parent"
android:layout_height= "Wrap_content"
android:orientation= "Vertical" >
<button
Android:id= "@+id/button"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
Android:text= "Show hidden content"/>
<viewstub
Android:id= "@+id/id_viewstub"
Android:layout_width= "Match_parent"
android:layout_height= "Wrap_content"
android:layout= "@layout/title"/>
The following is the Mainactivity.java main interface file:
public class Mainactivity extends activity {
Private Button B;
private viewstub stub;
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
b= (Button) Findviewbyid (R.id.button);
stub= (viewstub) Findviewbyid (r.id.id_viewstub);
B.setonclicklistener (New Onclicklistener () {
public void OnClick (View v) {
Stub.inflate ();
}
});
}
}
-->