【问题标题】:Android <include/> works but ViewStub doesn't, why?Android <include/> 可以工作,但 ViewStub 不能,为什么?
【发布时间】:2016-05-19 14:47:16
【问题描述】:

我在 FrameLayout 中使用 ViewStub,以便在第一次打开应用程序时使用教程视图对其进行填充。

我的活动.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>

我正在膨胀的视图称为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>

在我的 Activity onCreate 中,我使用了两种不同的方法来为 ViewStub 充气,但它们都不起作用(我看不到 intro_landing1 视图)。

第一种方法 - 设置可见性:

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 记录了返回的视图(inflatedView),它返回了 intro_landing1 RelativeLayout 的标签“RelativeLayoutIntroViewTag”,因此实际上返回了视图,但我没有看到它。

为了确保在视图树层次结构中正确定位 ViewStub,我在 Activity.xml 中使用了 &lt;include/&gt; 标记,而不是像这样的 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?

谢谢!

【问题讨论】:

  • 首选方式是致电inflate
  • 谢谢,我编辑了这个问题。简而言之:没有任何改变
  • 你检查了inflate调用返回值的内容吗?
  • 是的,我打印了 ViewStub.inflate() 返回的 View 的 View.getTag() ,这是我为 intro_landing1 RelativeLayout 设置的标签。
  • @tynn 感谢您的反馈,我重写了问题。希望它更容易理解。

标签: android xml android-layout viewstub


【解决方案1】:

所以我无法重现您的问题。我将在这里分享我的裸代码,因为它适用于我的设置。
我怀疑您可能在某处有一些额外的代码可能会错误地与您尝试执行的操作进行交互。 (我试图将其设置为尽可能接近问题)

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>

存根.xml

<?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>

MainActivity.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);
    }
}

附:我通常链接到一个演示项目,但在这种情况下,没有太多代码,如果有人看到答案,它不会依赖于未来的链接。希望对你有帮助

【讨论】:

    【解决方案2】:

    希望这有助于您了解ViewStub 的行为
    2. ViewStub 不能在不设置布局资源和通货膨胀的情况下改变可见性。所以一旦你设置了布局资源,你就可以inflatesetVisibility(View.VISIBLE);
    3. 当inflate() 方法被调用时,ViewStub 从其父级中移除并替换为正确的视图(您的布局的根视图)。
    了解更多How to use View Stub in android

    【讨论】:

      【解决方案3】:

      事实证明我的实现是正确的,但发生的事情是这样的:

      在启动我的应用程序时打开教程视图(膨胀 ViewStub)并将先前启动的内容写入共享首选项(SP),然后立即重新启动活动(再次运行 onCreated)但这次教程视图没有膨胀因为 if 语句失败了。

      这种情况仅在应用程序第一次打开时发生,因此并不清楚这是问题所在

      我仍然不知道为什么它在第一次打开应用程序时会重新启动活动,但我认为这是由于内存问题。

      只有在用户按照步骤操作后,我才通过将教程通货膨胀标志写入 SP 来解决此问题。

      由于@Alex.F 的答案实际上是正确的,我将其标记为 awnser。

      【讨论】:

        猜你喜欢
        • 2020-05-31
        • 1970-01-01
        • 1970-01-01
        • 2013-03-21
        • 1970-01-01
        • 2015-01-25
        • 2013-04-09
        • 2019-01-21
        • 1970-01-01
        相关资源
        最近更新 更多