我在 FrameLayout 中使用 ViewStub,这样我就可以在应用程序第一次打开时使用教程 View 对其进行扩充。
我的 Activity.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent">
...
<ViewStub
android:id="@+id/introHolder"
android:inflatedId="@+id/introHolder"
android:layout="@layout/intro_landing1"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
我正在膨胀的 View 称为 intro_landing1.xml
它是一个 RelativeLayout。
intro_landing1.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent" android:background="@color/opaqBlack30"
android:id="@+id/introView1"
android:tag="RelativeLayoutIntroViewTag">
<!--...-->
</RelativeLayout>
在我的 onCreate Activity 中,我使用了两种不同的方法来膨胀 ViewStub,但它们都不起作用(我看不到 intro_landing1 View )。
第一种方法 - 设置可见性:
if(!previouslyStarted){
((ViewStub) findViewById(R.id.introHolder)).setVisibility(View.VISIBLE);
}
第二种方法 - 膨胀 ViewStub:
ViewStub introStub = (ViewStub) findViewById(R.id.introHolder);
introStub.setLayoutResource(R.layout.intro_landing1);
View inflatedView = introStub.inflate();
使用第二种方法,我通过执行 inflatedView.getTag 记录了返回的 View (inflatedView),它返回了 intro_landing1 RelativeLayout 的标签“RelativeLayoutIntroViewTag”,因此 View 实际上已返回,但我没有看到它。
为了确保我在 View 树层次结构中正确定位了 ViewStub,我使用了 <include/>
Activity.xml 中的标记而不是像这样的 ViewStub:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent">
...
<!-- <ViewStub
android:id="@+id/introHolder"
android:inflatedId="@+id/introHolder"
android:layout="@layout/intro_landing1"
android:layout_width="match_parent"
android:layout_height="match_parent" />-->
<include layout="@layout/intro_landing1"/>
</FrameLayout>
这行得通。
为什么在可见性变化或膨胀后不显示 ViewStub?
谢谢!
最佳答案
所以我无法重现您的问题。我将在这里分享我的裸代码,因为它适用于我的设置。
我怀疑您可能在某处有一些额外的代码,这些代码可能会错误地与您尝试做的事情进行交互。 (我试图尽可能接近问题来设置它)
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.myexample.helloworld.MainActivity">
<ViewStub
android:id="@+id/myViewStub"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:inflatedId="@+id/myInflatedViewStub"
android:visibility="gone"
android:layout="@layout/stub"/>
</FrameLayout>
stub 文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="100dp"
android:text="Large Text"
android:id="@+id/textView"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"/>
</RelativeLayout>
主 Activity .java
public class MainActivity
extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
((ViewStub) findViewById(R.id.myViewStub)).setVisibility(View.VISIBLE);
}
}
附言我通常链接到一个演示项目,但在这种情况下,代码不多,如果有人看到答案,将来也不会依赖链接。希望对你有帮助
关于Android <include/> 工作但 ViewStub 不工作,为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37326765/