小编Yah*_*uez的帖子

如何在 MainActivity(或应用程序)开始时隐藏片段?

好吧,标题说明了一切,我不知道如何在启动时隐藏它。我尝试搜索,但没有发生我希望它发生的方式。

所以我想要发生的是我希望在启动时隐藏 BottomFragment。我希望它在我单击 TopFragment 中的此按钮时显示,并且 TopFragment 在启动时可见。

我的 MainActivity.xml 代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="rjj.tutorial_fragmentsthe3rd.MainActivity">

    <fragment
        class="rjj.tutorial_fragmentsthe3rd.TopFragment"
        android:id="@+id/topFragment"
        android:layout_width="match_parent"
        android:layout_height="300dp" />
    <fragment
        class="rjj.tutorial_fragmentsthe3rd.BottomFragment"
        android:id="@+id/bottomFragment"
        android:layout_width="match_parent"
        android:layout_height="300dp"
        android:layout_alignParentBottom="true"
        android:layout_alignParentStart="true"
        android:layout_below="@+id/topFragment" />

</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

我的 TopFragment.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">

    <Spinner
        android:id="@+id/origin"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:entries="@array/origins"/>

    <Spinner
        android:id="@+id/destination"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/origin"
        android:layout_alignParentStart="true"
        android:layout_marginTop="41dp"
        android:entries="@array/destinations"/>

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/destination"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="80dp"
        android:text="@string/button_caption" />
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

我的BottomFragment.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"> …
Run Code Online (Sandbox Code Playgroud)

java android android-fragments

6
推荐指数
1
解决办法
4237
查看次数

标签 统计

android ×1

android-fragments ×1

java ×1