Viewstub itself only occupies a little memory space
The layout that the layout property points to is loaded after calling inflate to replace the position of the current viewstub
When you need to choose what layout to display, you can use viewstub to delay loading
Note: Inflate can only be used once
Layout:activity_view_stub.xml
<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android" android:layout_width= " Match_parent " android:layout_height=" match_parent "> <button android:id=" @+id/btn_show " Android:layout_width= "Wrap_content" android:layout_height= "wrap_content" android:onclick= "show" Android:text= "show viewstub points to Layout"/> <button android:id= "@+id/btn_hide" android:layout_ Width= "Wrap_content" android:layout_height= "wrap_content" android:layout_torightof= "@id/btn_show" android:onclick= "Hide" android:text= "Hide viewstub point to Layout"/> <viewstub android:id= "@ +id/vs_stub " android:layout_width=" fill_parent " android:layout_height=" Fill_parent " android: layout_below= "@id/btn_show" android:layout= "@layout/rorate_bitmap"/></relativelayout>
Public classViewstubactivityextendsActivity { Viewstubmviewstub; @Override protected voidonCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate); Setcontentview (r.layout.activity_view_stub); mviewstub= (viewstub) Findviewbyid (r.id.vs_stub); } ViewInflate;Public voidShow(View view) {if(Inflate==NULL)Inflate=mviewstub. Inflate ();Else Inflate. setvisibility (View.VISIBLE); }Public voidHide(View view) {if(Inflate==NULL)Inflate=mviewstub. Inflate (); Inflate. setvisibility (View.GONE); }}
Android viewstub Layout Delay loading