Many times in the development of this situation, we generally do not need to display so many controls on the interface, but these controls in special cases are also needed to be displayed, at this time viewstub come in handy, say not much, direct code explanation
First we define a layout (that is, the layout that is hidden and sometimes displayed) Viewstub.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" > <edittext android:id= "@+id/text1" android:hint= "@string/moremsg" Android:layout_width= "Fill_parent" android:layout_height= "wrap_content"/> <edittext android: Id= "@+id/text2" android:hint= "@string/moremsg" android:layout_width= "Fill_parent" android: layout_margintop= "10DP" android:layout_height= "wrap_content"/> <edittext android:id= "@+id /text3 " android:hint=" @string/moremsg " android:layout_width=" fill_parent " android:layout_ margintop= "10DP" android:layout_height= "wrap_content"/> </LinearLayout>
The effect is simple, that is, three vertically arranged input boxes.
The next step is to define the main layout (note that viewstub is just a control, so his usage is no different from the button)
<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:orien tation= "vertical" android:id= "@+id/linear" tools:context= "com.example.viewstub.MainActivity" > <edittext android:hint= "Enter name" android:layout_width= "Fill_parent" android:layout_height= "Wrap_content"/> <edittext android:hint= "Enter phone here" android:layout_margintop= "10DP" android:layout_width= "Fill_pare NT "android:layout_height=" Wrap_content "/> <button android:id=" @+id/more "android:t Extsize= "25SP" android:layout_width= "fill_parent" android:layout_height= "Wrap_content" android:text= " Load more "/> <viewstub android:id=" @+id/viewstub "android:layout_width=" Fill_parent "Android : layout= "@layout/viewstub" android:layout_height= "Wrap_content"/></linearlayout>
Then start in the main function call:
Package Com.example.viewstub;import Android.support.v7.app.actionbaractivity;import Android.os.bundle;import Android.view.menu;import Android.view.menuitem;import Android.view.view;import Android.view.View.OnClickListener; Import Android.view.viewstub;import Android.widget.button;import Android.widget.edittext;import Android.widget.linearlayout;public class Mainactivity extends Actionbaractivity {private viewstub viewstub;private EditText Text1, Text2, text3;private Button more;private int flag; Whether the tag has been expanded hidden items 1 represents the private View layout; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate ( Savedinstancestate); Setcontentview (r.layout.activity_main); viewstub = (viewstub) Findviewbyid (R.id.viewstub); This.more = (Button) Findviewbyid (r.id.more); flag = 0;this.more.setonclicklistener (new Onclicklistener () {@ overridepublic void OnClick (View v) {//TODO auto-generated method stubif (flag = = 0) {//Viewstub.setvisibility (View.visi BLE); if (viewstub! = null) {View view = ViewstuB.inflate (); Text1 = (EditText) View.findviewbyid (R.ID.TEXT1); text2 = (EditText) View.findviewbyid (r.id.text2); Text3 = (EditText) View.findviewbyid (R.ID.TEXT3); view = null;} Flag = 1;more.settext ("close More");} else {flag = 0;viewstub.setvisibility (view.invisible); More.settext ("Load More");}});}}
As follows:
Android Layout optimized--viewstub use