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

Android Studio底部导航栏异常:Fragment未在指定区域加载求助

问题解决:BottomNavigationView加载Fragment位置异常

你的问题核心原因是Fragment替换时指定了错误的容器ID,导致Fragment没有加载到预留的FrameLayout中,而是错误挂载到了底部导航栏的菜单项视图上,这就是显示在home图标上方的原因。

错误点定位

在replaceFragment方法中,你错误使用了底部导航菜单项的ID作为Fragment容器:

fragmentTransaction.replace(R.id.history, fragment);

而布局中专门用来承载Fragment的FrameLayout ID是flFragment,R.id.history只是导航菜单里的菜单项ID,并非布局容器ID。

修复方案

修改replaceFragment方法,将替换目标改为正确的FrameLayout容器ID:

private void replaceFragment(Fragment fragment){
    FragmentManager fragmentManager=getSupportFragmentManager();
    FragmentTransaction fragmentTransaction=fragmentManager.beginTransaction();
    // 替换为布局中预留的Fragment容器ID
    fragmentTransaction.replace(R.id.flFragment, fragment);
    fragmentTransaction.commit();
}

额外检查项

  • 确保homeFragment和historyFragment的布局文件没有设置固定位置或偏移,保持根布局宽高为match_parent或合理的自适应属性
  • 确认底部导航菜单文件(bottom_navigation.xml)中的菜单项IDhome、history与Activity中的判断逻辑完全对应

内容的提问来源于stack exchange,提问作者Himanshu Kumar

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.12 21:13:26