我同时实现了一个带有
DrawerNavigationView
的BottomNavigationView
,但问题是ActionBar
...默认片段是好的但是改变fragment
它创建了一个奇怪的白线ActionBar
的顶部,它不再位于正确的位置......你知道为什么吗?谢谢你的帮助D:
片段的主要活动容器的 XML 代码
<androidx.drawerlayout.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/drawerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/nav_view"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="0dp"
android:layout_marginEnd="0dp"
android:background="?android:attr/windowBackground"
android:theme="@style/Theme2"
app:itemIconTint="@color/white_darker"
app:itemRippleColor="@color/white_darker"
app:itemTextColor="@color/white_darker"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:menu="@menu/bottom_nav_menu" />
<fragment
android:id="@+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:navGraph="@navigation/mobile_navigation" />
</androidx.constraintlayout.widget.ConstraintLayout>
<com.google.android.material.navigation.NavigationView
android:id="@+id/navigation_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:layout_marginTop="?android:attr/actionBarSize"
android:fitsSystemWindows="true"
app:headerLayout="@layout/drawer_header"
app:itemTextAppearance="?textAppearanceListItem"
app:menu="@menu/navigation_drawer" />
问题片段的 XML 代码(Empty ConstraintLayout)
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/dark"
tools:context=".HomeUi.Notifications.NotificationsFragment">
在您的
styles.xml
文件中,在NoActionBar
之后尝试parent
。其他问题可能是您的linearlayout
中有一个fragment
的其余部分。
对于将来遇到同样问题的人,我找到了解决方案,将 CoordinatorLayout 作为根添加,将所有 xml 代码包装在主片段容器上,并在其上使用 android:fitsSystemWindows="true",现在一切正常!
对于使用
CoordinatorLayout
和 AppBarLayout
在普通活动中体验它的任何人。在 android:fitsSystemWindows="true"
和 CoordinatorLayout
中设置 AppBarLayout
就可以了。
<androidx.coordinatorlayout.widget.CoordinatorLayout
android:fitsSystemWindows="true"
>
<com.google.android.material.appbar.AppBarLayout
android:fitsSystemWindows="true"
>
</com.google.android.material.appbar.AppBarLayout>
...
</androidx.coordinatorlayout.widget.CoordinatorLayout>