无法删除 nav_host_fragment 之上的 space
Can't remove space on top of nav_host_fragment
我刚刚实现了底部导航(AS 的默认设置 - 文件 -> 新建 -> Activity -> 底部导航 Activity)除了顶部的 space 之外一切都很好的 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
和顶部之间的 space 仍然存在。
这是布局文件:
<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>
来自@Mike 的回答
That looks like the android:paddingTop="?attr/actionBarSize" on the
<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>
从您的相对布局中删除这一行。
android:paddingTop="?attr/actionBarSize"
在我的布局中,我也有 space 导航主机片段的底部,我计算出轨迹。
<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>
我刚刚实现了底部导航(AS 的默认设置 - 文件 -> 新建 -> Activity -> 底部导航 Activity)除了顶部的 space 之外一切都很好的 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
和顶部之间的 space 仍然存在。
这是布局文件:
<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>
来自@Mike 的回答
That looks like the android:paddingTop="?attr/actionBarSize" on the
<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>
从您的相对布局中删除这一行。
android:paddingTop="?attr/actionBarSize"
在我的布局中,我也有 space 导航主机片段的底部,我计算出轨迹。
<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>