Viewstub can dynamically add layouts at run time. The help document is given a definition of:
"A Viewstub is a invisible, zero-sized View that can being used to lazily inflate layout resources at runtime." When a viewstub was made visible, or when inflate () was invoked, the layout resource is inflated. The viewstub then replaces itself in it parent with the inflated View. Therefore, the viewstub exists in the view hierarchy until setvisibility (int) or inflate () is invoked. The inflated View is added to the viewstub's parent with the Viewstub ' s layout parameters. Similarly, you can define/override the inflate View ' s ID by using the Viewstub ' Inflatedid property.
Allin all: Viewstub can give other view prior positions, call Inflater () or the setvisible () method when needed to display these view components.
Layout.xml:
<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=" Match_parent "Android:o" rientation= "Vertical" tools:context= ".
Mainactivity "> <linearlayout android:layout_width=" match_parent "android:layout_height=" Wrap_content " android:orientation= "Horizontal" > <button android:id= "@+id/showbtn" android:layout_width= "Match_" Parent "android:layout_height=" 50dip "android:layout_weight=" 1 "android:text=" show "/> <butto n android:id= "@+id/deletebtn" android:layout_width= "match_parent" android:layout_height= "50dip" an droid:layout_weight= "1" android:text= "delete"/> </LinearLayout> <linearlayout Android:layout_w
Idth= "Wrap_content" android:layout_height= "wrap_content" > <viewstub android:id= "@+id/viewstub" Android:layouT_width= "Wrap_content" android:layout_height= "wrap_content" android:layout= "@layout/next"/> </Li
Nearlayout> </LinearLayout>
Next.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:o" rientation= "vertical" > <progressbar android:id= @+id/progressbar1 "style="
? android:attr/ Progressbarstylelarge "
android:layout_width=" wrap_content "
android:layout_height=" wrap_content "/>"
</LinearLayout>
Main.java:
Package com.example.android_viewstub1;
Import Android.os.Bundle;
Import Android.annotation.SuppressLint;
Import android.app.Activity;
Import Android.view.Menu;
Import Android.view.View;
Import Android.view.View.OnClickListener;
Import android.view.ViewStub;
Import Android.widget.Button;
public class Mainactivity extends activity {Button btn1, btn2;
Viewstub viewstub;
@Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
BTN1 = (Button) This.findviewbyid (R.ID.SHOWBTN);
BTN2 = (Button) This.findviewbyid (R.ID.DELETEBTN);
Viewstub = (viewstub) This.findviewbyid (r.id.viewstub); Btn1.setonclicklistener (New Onclicklistener () {@SuppressLint ("Newapi") @Override public void OnClick (Vi
EW arg0) {viewstub.setvisibility (view.visible);
}
});
Btn2.setonclicklistener (New Onclicklistener () {@SuppressLint ("Newapi") @Override public void OnClick (View arg0) {viewstub.setvisibility (view.invisible);
}
});
}
}
Effect:
The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.