使用带有Android导航UI库的BottomNavigationView进行导航时,从操作栏中删除向上按钮 [英] Remove up button from action bar when navigating using BottomNavigationView with Android Navigation UI library

查看:100
本文介绍了使用带有Android导航UI库的BottomNavigationView进行导航时,从操作栏中删除向上按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个小应用程序,其中包含三个片段,用于通过BottomNavigationView进行顶层导航.如果启动该应用程序并单击底部导航栏上的导航按钮,则会在操作栏中显示一个向上按钮.这是该活动的代码:

I've created a small app that has three fragments for top-level navigation through a BottomNavigationView. If you launch the app and click on a navigation button on the bottom nav, you are presented with an up button in the action bar. Here is the code for the activity:

class MainActivity : AppCompatActivity() {

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

        setSupportActionBar(toolbar)

        val navController = navHostFragment.findNavController()
        setupActionBarWithNavController(this, navController)
        setupWithNavController(bottomNav, navController)
    }

    override fun onSupportNavigateUp(): Boolean
            = findNavController(navHostFragment).navigateUp()

}

这是结果的屏幕截图.该应用程序在主屏幕上启动,我只需单击BottomNavigationView中的配置文件按钮即可.

Here is a screenshot of the result. The app is launched on the home screen and all I've done is simply click the profile button from the BottomNavigationView.

我尝试听过BottomNavigationView的项目选择,并使用不同的 NavOptions 无济于事.我们有什么办法可以避免在用户使用BottomNavigationView导航时在操作栏中显示向上按钮?

I've tried listening to the BottomNavigationView's item selections and navigating manually using different NavOptions to no avail. Is there anything we can do to avoid showing an up button in the action bar while the user is navigating with a BottomNavigationView?

推荐答案

从1.0.0-alpha07开始,您可以使用AppBarConfiguration来配置该行为.

Starting with 1.0.0-alpha07 you can use AppBarConfiguration to configure that behaviour.

AppBarConfiguration具有一个Builder构造函数,因此您可以创建一个新的Builder,其中包含一组特定的顶级目标,这些目标由其id引用(此id是您在导航布局上设置的目标).

AppBarConfiguration has a Builder constructor so you can create a new Builder with a specific set of top level destinations, referenced by their id (this id is the one you set on your navigation layout).

创建新的AppBarConfiguration:

val appBarConfiguration = AppBarConfiguration
            .Builder(
                    R.id.navigationHomeFragment,
                    R.id.navigationListFragment,
                    R.id.navigationProfileFragment)
            .build()

然后,您需要调用setupActionBarWithNavController(this, navController, appBarConfiguration)

这是处理最佳导航行为的正确方法.

This is the right way to handle top navigation behaviours.

这篇关于使用带有Android导航UI库的BottomNavigationView进行导航时,从操作栏中删除向上按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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