【问题标题】:Android Studio - Disabling back button on default Navigation DrawerAndroid Studio - 禁用默认导航抽屉上的后退按钮
【发布时间】:2020-07-02 12:00:15
【问题描述】:

所以我创建了默认的导航抽屉活动,并在底部添加了我自己的按钮来手动更改片段。

但是,我不希望这些按钮添加到后退堆栈中,因为我希望导航菜单始终显示汉堡菜单而不是后退按钮。菜单我本质上是想禁用后退按钮和堆栈在我显示时更改导航抽屉上的片段。我将如何实现这一目标?

https://imgur.com/a/B8G0hqP

这是在 onCreate 的导航活动中

        setContentView(R.layout.activity_navigation);
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        DrawerLayout drawer = findViewById(R.id.drawer_layout);
        NavigationView navigationView = findViewById(R.id.nav_view);
        // Passing each menu ID as a set of Ids because each
        // menu should be considered as top level destinations.
        mAppBarConfiguration = new AppBarConfiguration.Builder(
                R.id.nav_home, R.id.nav_projects,
                R.id.nav_tools, R.id.nav_share, R.id.nav_send)
                .setDrawerLayout(drawer)
                .build();
        this.navController = Navigation.findNavController(this, R.id.nav_host_fragment);
        NavigationUI.setupActionBarWithNavController(this, this.navController, mAppBarConfiguration);
        NavigationUI.setupWithNavController(navigationView, this.navController);

【问题讨论】:

    标签: android android-studio user-interface android-fragments android-activity


    【解决方案1】:

    使用 NavController.OnNavigatedListener 更新工具栏图标。

    val navController = Navigation.findNavController(this, R.id.fragment_main_navHost)
    navController.addOnNavigatedListener(contoller, destination -> {
       if(destination.id == R.id.fragmentTwo){
              // update the toolbar here
    }
        })
    

    【讨论】:

      【解决方案2】:

      试试这些:

      getSupportActionBar().setDisplayHomeAsUpEnabled(false); 
      getSupportActionBar().setDisplayShowHomeEnabled(false);
      

      【讨论】:

        猜你喜欢
        • 2015-04-11
        • 1970-01-01
        • 2016-11-01
        • 1970-01-01
        • 2020-12-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多