Android Studio底部导航栏异常: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
相关产品推荐
相关产品推荐

