📜  如何添加屏幕但不在抽屉中显示 (1)

📅  最后修改于: 2023-12-03 15:09:12.775000             🧑  作者: Mango

如何添加屏幕但不在抽屉中显示

在Android开发中,有些时候我们需要添加一个新的屏幕,但是又不想把它显示在抽屉中。下面我们来看一下具体的实现方式。

使用Navigation Component

使用Navigation Component可以方便地对屏幕之间的跳转进行管理。我们可以在Navigation Graph中添加一个Fragment,并设置它的属性visibility="gone",这样它就不会显示在抽屉中了。

<fragment
    android:id="@+id/my_screen"
    android:name="com.example.MyScreenFragment"
    android:visibility="gone" />
使用BottomNavigationView

如果你正在使用BottomNavigationView实现底部导航栏的效果,那么你可以在设置Item时设置对应的Fragment,并且将它的position设置为足够大的值,这样它就不会显示在导航栏中了。

bottomNavigationView.getMenu().add(
        Menu.NONE
        R.id.action_my_screen,
        Menu.NONE,
        R.string.my_screen_title)
        .setCheckable(true)
        .setIcon(R.drawable.ic_my_screen)
        .setChecked(false)
        .setPosition(Integer.MAX_VALUE);
使用DrawerLayout

在使用DrawerLayout实现抽屉效果的时候,我们可以在添加新的屏幕时,设置它的layout_gravity属性为start,这样它就会出现在电话号码编辑、设置和其他抽屉菜单之后了。

<fragment
    android:id="@+id/my_screen"
    android:name="com.example.MyScreenFragment"
    android:layout_gravity="start" />

以上就是实现添加屏幕但不在抽屉中显示的三种方法,可以根据具体需求进行选择。