ViewStub可以在运行时动态的添加布局。帮助文档给定的定义是:
"A ViewStub is an invisible, zero-sized View that can be used to lazily inflate layout resources at runtime. When a ViewStub is made visible, or when inflate() is invoked, the layout resource is inflated. The ViewStub then replaces itself in its parent with the inflated View or Views. 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's inflatedId property.”
总之是:ViewStub可以给其他的view事先占据好位置,当需要的时候调用inflater()或者是setVisible()方法显示这些View组件。
layout.xml:
<LinearLayout xmlns:android="http://www.easck.com/apk/res/android"
xmlns:tools="http://www.easck.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="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" />
<Button
android:id="@+id/deleteBtn"
android:layout_width="match_parent"
android:layout_height="50dip"
android:layout_weight="1"
android:text="delete" />
</LinearLayout>
<LinearLayout
android:layout_width="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" />
</LinearLayout>
</LinearLayout>
next.xml:










