You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用BottomNavView和AppBarConfiguration时如何隐藏Toolbar中的Fragment名称/ID

解决Toolbar显示Fragment名称的几种方法

方法1:直接在Fragment里取消标题设置

如果你的Fragment代码里写了getActivity().setTitle("XXX")这类设置标题的代码,直接删掉这行,或者改成getActivity().setTitle("");,就能避免把Fragment名称传到Toolbar上。

方法2:在Activity里强制固定Toolbar标题

在Activity初始化Toolbar的代码段中,直接给Toolbar设置空标题或者你需要的固定内容,覆盖Fragment的标题设置:

// 假设已获取Toolbar实例
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
// 设置为空标题,或替换成你想要的固定文字
getSupportActionBar().setTitle("");

方法3:Navigation Component场景下的处理

如果你的Bottom Navigation是和Navigation组件配合使用的:

  • 打开导航图文件(nav_graph.xml),找到对应的Fragment节点,删除它的label属性,或者把属性值设为空字符串;
  • 或者在Activity配置导航逻辑时,不让NavController自动控制Toolbar标题:
    NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
    AppBarConfiguration appBarConfiguration = new AppBarConfiguration.Builder(
            R.id.nav_home, R.id.nav_dashboard, R.id.nav_notifications)
            .build();
    // 仅关联导航按钮,不自动设置标题
    NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);
    // 手动设置Toolbar标题为空
    getSupportActionBar().setTitle("");
    

方法4:手动切换Fragment时控制标题

如果是用FragmentTransaction手动切换Fragment,在切换完成后,手动设置Toolbar标题为空:

getSupportFragmentManager().beginTransaction()
        .replace(R.id.fragment_container, new HomeFragment())
        .commit();
// 切换完成后重置标题
getSupportActionBar().setTitle("");

内容的提问来源于stack exchange,提问作者Farooq Ahmad

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.08 21:21:02