将底部菜单栏与导航体系结构组件一起使用时,如何从工具栏中删除后退按钮 [英] How to remove back button from toolbar when using bottom menu bar with navigation architecture components

查看:93
本文介绍了将底部菜单栏与导航体系结构组件一起使用时,如何从工具栏中删除后退按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有底部菜单栏的应用程序,用户可以使用该菜单栏在4个主页选项卡之间切换.像下面这样工作正常.

I have an application which has a bottom menu bar which users can use to switch between 4 home page tabs. It's working fine like below.

我唯一的问题是当我在不同片段之间切换时,它显示后退按钮.由于所有这些片段都处于同一级别,所以我不希望它表现那样.

The only problem I'm having is it showing back button when I switch between different fragment. Since all these fragments are at the same level I do not want it to behave like that.

这是我的实现.

MainNavigationActivity

class MainNavigationActivity : AppCompatActivity() {

    private lateinit var navigationController: NavController

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        initialization()
    }

    private fun initialization(){

        navigationController = Navigation.findNavController(this, R.id.hostFragment)

        setSupportActionBar(toolbar)
        NavigationUI.setupWithNavController(bottomNavigationBar,navigationController)
        NavigationUI.setupActionBarWithNavController(this,navigationController)
    }

override fun onBackPressed() {
        onSupportNavigateUp()
    }

MainNavigationActivity布局

<RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".activities.MainNavigationActivity">

    <androidx.appcompat.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:background="@color/colorPrimary"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
            app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"/>

    <fragment
            android:id="@+id/hostFragment"
            android:name="androidx.navigation.fragment.NavHostFragment"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/toolbar"
            android:layout_above="@id/bottomNavigationBar"
            app:defaultNavHost="true"
            app:navGraph="@navigation/main_navigation_graph" />

    <com.google.android.material.bottomnavigation.BottomNavigationView
            android:id="@+id/bottomNavigationBar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:background="@android:color/white"
            app:menu="@menu/bottom_navigation_menu"
            app:labelVisibilityMode="labeled"/>

</RelativeLayout>

bottom_navigation_menu.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item
            android:id="@+id/navigation_home"
            android:state_checked="true"
            android:color="@color/colorPrimary"
            android:title="@string/navigation_home"
            android:icon="@drawable/ic_bottom_bar_menu_home"/>
    <item
            android:id="@+id/navigation_offers"
            android:state_checked="false"
            android:color="@color/gray"
            android:title="@string/navigation_offers"
            android:icon="@drawable/ic_bottom_bar_menu_offers"/>

    <item
            android:id="@+id/navigation_my_bookings"
            android:state_checked="false"
            android:color="@color/gray"
            android:title="@string/navigation_my_bookings"
            android:icon="@drawable/ic_bottom_bar_menu_bookings"/>
    <item
            android:id="@+id/navigation_my_account"
            android:state_checked="false"
            android:color="@color/gray"
            android:title="@string/navigation_my_account"
            android:icon="@drawable/ic_bottom_bar_menu_account"/>
</menu>

ID被赋予导航图中的片段,而menu.xml中的ID相同.这样便可以识别正确的片段并正确切换到该片段.

The Ids are given to the fragments in the navigation graph and the ids in the menu.xml are the same. that's how it identifies the correct fragment and switch to that fragment correctly.

如何在主屏幕级别上删除工具栏上的此后退按钮?

How can I remove this back button on the toolbar in home screen level?

推荐答案

按照 NavigationUI文档:

默认情况下,当用户位于导航图的顶级目标位置时,导航"按钮处于隐藏状态,而在其他任何目标位置均显示为向上"按钮.

By default, the Navigation button is hidden when a user is at a top-level destination of a navigation graph and appears as an Up button in any other destination.

如果要自定义哪些目标被视为顶级目标,则可以将一组目标ID传递给构造函数,如下所示:

If you want to customize which destinations are considered top-level destinations, you can instead pass a set of destination IDs to the constructor, as shown below:

val appBarConfiguration = AppBarConfiguration(setOf(
    R.id.navigation_home, R.id.navigation_offers,
    R.id.navigation_my_bookings, R.id.navigation_my_account))

(请注意,此构造函数需要navigation-ui-ktx工件-替代方法是使用AppBarConfiguration.Builder)

(Note that this constructor requires the navigation-ui-ktx artifact - the alternative is to use the AppBarConfiguration.Builder)

这篇关于将底部菜单栏与导航体系结构组件一起使用时,如何从工具栏中删除后退按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆