Like the documentation says, ViewStub
is a View
that is inflated lazily.
You can declare a ViewStub
in an XML file like this:
<ViewStub android:id="@+id/stub"
android:inflatedId="@+id/subTree"
android:layout="@layout/mySubTree"
android:layout_width="120dip"
android:layout_height="40dip" />
The android:layout
attribute is a reference to the View
that will be inflated next to a call of inflate()
. So
ViewStub stub = (ViewStub) findViewById(R.id.stub);
View inflated = stub.inflate();
When the method inflate()
is invoked the ViewStub
is removed from its parent and replaced with the right View
(the root view of mySubTree
layout).
If you want to do this progammatically then your code should be something like:
ViewStub stub = new ViewStub(this);
stub.setLayoutResource(R.layout.mySubTree);
stub.inflate();
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…