【发布时间】:2016-05-25 15:12:57
【问题描述】:
这是我的用例:
我想在运行时更改我的膨胀布局,比如首先我膨胀布局 a,然后过一段时间我想显示布局 B,然后是布局 C,等等。
我在某处读到,我应该使用viewstub 和inflate,而不是在主布局中包含布局然后隐藏/取消隐藏。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ViewStub
android:id="@+id/layout_stub"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
我现在的问题是,当我为第一个布局充气时,它工作正常,但下一次当我尝试为第二个布局充气时,我得到了存根为空。
ViewStub stub = (ViewStub) findViewById(R.id.layout_stub);
stub.setLayoutResource(layoutId);
View inflated = stub.inflate();
我的理解是 Viewstub 是一个正在加载布局的容器,如果是这样的话 为什么我在尝试加载第二个布局时没有得到 ViewStub? (所以这意味着当我膨胀第一个布局(A)时,放置 ViewStub 的布局被完全删除了?)
我正在寻找使用 Viewstub 或替代方案来实现我的用例的任何指针。
【问题讨论】: