无法移除nav_host_fragment顶部的空白

28

我刚刚实现了底部导航(AS默认-文件->新建->活动->底部导航活动)。除了在 nav_host_fragment 顶部有一段空白之外,一切都很好。

wrong space

由于它是在 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>

3
这看起来像是在<RelativeLayout>上使用了android:paddingTop="?attr/actionBarSize" - Mike M.
你使用的是什么主题?也许是为“ActionBar”保留空间的主题? - Martin Marconcini
@MikeM。这正是导致空格的原因。我简直不敢相信我没有注意到!谢谢你! - Igor Grecco Lacourt
您可以将此评论发布为答案,以便我将其标记为此问题的正确答案。 - Igor Grecco Lacourt
没问题,我很好。 :-) 没什么大事,只是一点小疏忽。如果您愿意,可以自己发布答案或删除这个问题。不过还是谢谢您的提供,非常感激。祝一切顺利! - Mike M.
3个回答

69
从您的RelativeLayout中删除此行。
android:paddingTop="?attr/actionBarSize"

2
在2021年,Android Studio中默认的“底部导航活动”仍然会出于某种原因向BottomNavigationView的父布局添加paddingTop。 - tronman

2

@Mike的最初回答:

看起来这个是在android:paddingTop中使用了"?attr/actionBarSize"。

 <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"
        >

    <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>

0

在我的布局中,导航宿主片段底部也有空间,我计算了轨迹。

  <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <com.google.android.material.appbar.MaterialToolbar
            android:id="@+id/toolbarMain"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:layout_alignParentTop="true"
            android:background="@color/colorPrimary"
            app:contentInsetStart="@dimen/_minus10sdp">
    
            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginStart="0dp"
                android:layout_marginEnd="0dp"
                android:gravity="center"
                android:orientation="horizontal">
    
                <com.google.android.material.textview.MaterialTextView
                    android:id="@+id/txtTitleMain"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:gravity="center"
                    android:paddingStart="0dp"
                    android:paddingEnd="@dimen/_15sdp"
                    android:text="@string/app_name"
                    android:textAllCaps="false"
                    android:textColor="@color/colorWhite"
                    android:textSize="@dimen/_14ssp"
                    android:textStyle="bold" />
    
            </LinearLayout>
    
    
        </com.google.android.material.appbar.MaterialToolbar>
    
        <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_above="@+id/nav_view"
            android:layout_below="@id/toolbarMain"
            app:defaultNavHost="true"
            app:navGraph="@navigation/mobile_navigation" />
    
    
        <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:layout_marginStart="0dp"
            android:layout_marginEnd="0dp"
            android:background="?android:attr/windowBackground"
            app:menu="@menu/bottom_nav_menu" />
    
    </RelativeLayout>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接