我刚刚实现了底部导航(AS默认-文件->新建->活动->底部导航活动)。除了在 nav_host_fragment
顶部有一段空白之外,一切都很好。
由于它是在 ConstraintLayout 中生成的,我尝试清除约束并将顶部约束设置为parent
,将 margin
设置为“0dp”,并将 height
设置为 match_constraint
。
我不成功地删除了约束并一遍又一遍地尝试。
我使用了 Clean Project
。
我改为 RelativeLayout 并像这样设置参数:
<fragment
android:id="@+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_above="@+id/nav_view"
app:defaultNavHost="true"
app:navGraph="@navigation/mobile_navigation" />
但是 nav_host_fragment
和顶部之间的空间仍然存在。
这是布局文件:
<RelativeLayout 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:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="?attr/actionBarSize">
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/nav_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="?android:attr/windowBackground"
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"
android:layout_alignParentTop="true"
android:layout_above="@+id/nav_view"
app:defaultNavHost="true"
app:navGraph="@navigation/mobile_navigation" />
</RelativeLayout>
<RelativeLayout>
上使用了android:paddingTop="?attr/actionBarSize"
。 - Mike M.