如何从特定 Activity 中隐藏包含在 BaseActivity 布局中的片段

How to Hide a Fragment included in a BaseActivity layout from a specific Activity

这是我的问题,我正在为我的移动应用程序集成广告。

为了显示横幅,我创建了一个 BannerFragment

我想在我的所有活动中显示此横幅,因此我已将其包含在 ActionBarAbstractActivity 布局中。

这让我可以在我的所有活动中显示我的横幅,而无需处理他们的 XML 文件。

但是,我有一个特定的 Activity 我不想在其中展示广告。

这就是为什么我需要在 Activity 中隐藏横幅。如何访问此 Activity 中的 BannerFragment,即使它未在其 XML 布局中引用?

我试图通过 ID 找到片段并将其隐藏,但它不起作用。

我是否应该为此活动创建合适的横幅容器并将可见性设置为 "HIDE" 或 "GONE"?

也许你可以使用 include 片段的 xml 而不是 hide or gone。

像这样

/>

您可以将线性布局本身的可见性 属性 更改为 hide/show 片段。

在 XML 中为 LinearLayout 使用 id。

<LinearLayout 
    android:id="@+id/bannerLayout"
    android:layout_width="match_parent"
    android:layout_height="50dp">
    <fragment
     ..
     .. 
    />

</LinearLayout>

在你的activity,

LinearLayout  bannerLayout = (LinearLayout) findViewById(R.id.bannerLayout);

要隐藏布局,

bannerLayout.setVisibility(View.INVISIBLE); // Or in this case, View.GONE will also work

要显示布局,

bannerLayout.setVisibility(View.VISIBLE);