Android开发Fragment文本显示在导航栏问题排查求助
问题根因
- 布局层级错误:你将
FragmentContainerView写在了BottomNavigationView的标签内部,Fragment的渲染容器和底部导航栏属于同一层级,内容自然会叠在导航栏区域显示。同时CoordinatorLayout节点下多写了一个多余的右尖括号,且android:layout_above是RelativeLayout的专属属性,在CoordinatorLayout中不会生效。
- 布局层级错误:你将
- 导航逻辑冲突:你同时配置了Jetpack Navigation组件的
NavHostFragment和导航图,又手动实现了OnNavigationItemSelectedListener做Fragment切换,两套逻辑同时运行会产生不可预期的渲染问题。
- 导航逻辑冲突:你同时配置了Jetpack Navigation组件的
修复方案
第一步:修正activity_main.xml布局
将FragmentContainerView从BottomNavigationView中移出,调整约束让Fragment显示区域在顶部标题、中间输入框下方,底部导航栏上方:
<?xml version="1.0" encoding="utf-8"?> <androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:tools="http://schemas.android.com/tools" android:id="@+id/coordinatorLayout" xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent"> <androidx.constraintlayout.widget.ConstraintLayout android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <TextView android:id="@+id/titleTv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Sander's app" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" android:layout_marginTop="20dp"/> <EditText android:id="@+id/editTextTextPersonName" android:layout_width="match_parent" android:layout_height="103dp" android:ems="10" android:inputType="textPersonName" android:text="" android:background="#fff" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toBottomOf="@id/titleTv" android:layout_marginTop="16dp"/> <!-- Fragment容器移到导航栏外部,约束在输入框下方、导航栏上方 --> <androidx.fragment.app.FragmentContainerView android:id="@+id/fragmentContainerView" android:name="androidx.navigation.fragment.NavHostFragment" android:layout_width="match_parent" android:layout_height="0dp" app:defaultNavHost="true" app:navGraph="@navigation/my_nav" app:layout_constraintTop_toBottomOf="@id/editTextTextPersonName" app:layout_constraintBottom_toTopOf="@id/bottomNavigationView" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent"/> <com.google.android.material.bottomnavigation.BottomNavigationView android:id="@+id/bottomNavigationView" android:layout_width="match_parent" android:layout_height="wrap_content" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:menu="@menu/bottom_menu"/> </androidx.constraintlayout.widget.ConstraintLayout> </androidx.coordinatorlayout.widget.CoordinatorLayout>
第二步:修正MainActivity导航逻辑
删除手动实现的OnNavigationItemSelectedListener和手动切换Fragment的代码,直接用Jetpack Navigation和底部导航栏的绑定逻辑即可:
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); BottomNavigationView bottomNavigationView = findViewById(R.id.bottomNavigationView); NavHostFragment navHostFragment = (NavHostFragment) getSupportFragmentManager().findFragmentById(R.id.fragmentContainerView); NavigationUI.setupWithNavController(bottomNavigationView, navHostFragment.getNavController()); }
注意:需要保证bottom_menu.xml中item的id和导航图my_nav.xml中fragment的id完全一致,即可实现点击导航项自动切换Fragment的效果,不需要自己写切换逻辑。
内容的提问来源于stack exchange,提问作者helpmeplz16
相关产品推荐
相关产品推荐

