ViewStub本身只占一点内存空间
在调用inflate后 才加载layout属性所指向的layout, 以替换当前ViewStub所占的位置
在某些 需要选择显示何种布局时,就可以使用ViewStub来延迟加载
注意:inflate只能有一次
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="显示viewstub指向的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="隐藏viewstub指向的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 class ViewStubActivity extends Activity {
ViewStub mViewStub;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_stub);
mViewStub = (ViewStub) findViewById(R.id.vs_stub);
}
View inflate;
public void show(View view) {
if (inflate == null)
inflate = mViewStub.inflate();
else
inflate.setVisibility(View.VISIBLE);
}
public void hide(View view) {
if (inflate == null)
inflate = mViewStub.inflate();
inflate.setVisibility(View.GONE);
}
}
<p>版权声明:本文为博主原创文章,未经博主允许不得转载。</p>