如何在Android中使用View Stub [英] How to use View Stub in android
问题描述
我想在android中使用ViewStub,所以请帮助我.我已经创建
I want to use ViewStub in android, so please help me. I have created
ViewStub stub = new ViewStub;
View inflated = stub.inflate();
如何以编程方式使用它?
How to use it programmatically?
推荐答案
就像文档说,ViewStub
是一个懒惰地膨胀的View
.
Like the documentation says, ViewStub
is a View
that is inflated lazily.
您可以在XML文件中声明一个ViewStub
,如下所示:
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" />
android:layout
属性是对View
的引用,该引用将在inflate()
的调用旁边被夸大.所以
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();
调用方法inflate()
时,会将ViewStub
从其父级中移除,并替换为正确的View
(mySubTree
布局的根视图).
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();
这篇关于如何在Android中使用View Stub的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!