Reprint please mark: reproduced in http://www.cnblogs.com/Liuyt-61/p/6602926.html
----------------------------------------------------------------
> Lazy Loading with Viewstub
Role: The viewstub tag can be used as an include tag to introduce an external layout, but the layout introduced by Viewstub does not expand by default, does not occupy the display, and does not occupy the location, thus saving CPU and memory when parsing layout.
Common_text.xml<?xml version="1.0"encoding="Utf-8"? ><linearlayout xmlns:android="http://schemas.android.com/apk/res/android"Android:layout_width="match_parent"Android:layout_height="match_parent"android:orientation="Vertical"> <TextView android:layout_width="match_parent"Android:layout_height="wrap_content"Android:text="Hide Content"/></linearlayout>------------------------------------------------Main.xml<?xml version="1.0"encoding="Utf-8"? ><linearlayout xmlns:android="http://schemas.android.com/apk/res/android"Android:layout_width="match_parent"Android:layout_height="match_parent"android:orientation="Vertical"> <include layout="@layout/common_title"/> <framelayout android:layout_width="match_parent"Android:layout_height="wrap_content"> <TextView Android:id="@+id/textview"Android:layout_width="match_parent"Android:layout_height="wrap_content"Android:text="Body Content"android:textsize="16sp"/> <include layout="@layout/common_progress"/> </FrameLayout> <Button Android:id="@+id/button"Android:layout_width="wrap_content"Android:layout_height="wrap_content"Android:text="Show hidden content"/> <viewstub Android:id="@+id/viewstub"Android:layout_width="wrap_content"Android:layout_height="wrap_content"Android:layout="@layout/common_text"/></linearlayout>--------------------------------------mainactivity.javapackage com. Liuyt.s03_e22_include;import Android.app.activity;import Android.os.bundle;import Android.view.View;import Android.view.view.onclicklistener;import Android.view.viewstub;import Android.widget.Button; Public classMainactivity extends Activity {PrivateButton btn; Privateviewstub viewstub; @Overrideprotected voidonCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); BTN=(Button) Findviewbyid (R.id.button); Viewstub=(viewstub) Findviewbyid (r.id.viewstub); Btn.setonclicklistener (NewOnclicklistener () {@Override Public voidOnClick (View arg0) {//TODO auto-generated Method Stubviewstub.inflate (); } }); }}
[Android] Android Layout Optimization Viewstub