android 使用底部导航删除ConstraintLayout中的上边距

qhhrdooz  于 2023-04-10  发布在  Android
关注(0)|答案(2)|浏览(159)

我已经在Android Studio中创建了底部导航Activity。现在我正在调整所有布局以满足我的需求,但我在删除nav_host_fragment与其父级约束布局之间的边距时遇到了严重的问题。这会导致所有片段的布局显示不需要的偏移量或边距。
https://obrazki.elektroda.pl/6421823700_1611991870.png
布局定义如下

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
    android:paddingTop="?attr/actionBarSize">

    <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"
        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_constraintBottom_toTopOf="@id/nav_view"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:navGraph="@navigation/mobile_navigation" />

</androidx.constraintlayout.widget.ConstraintLayout>
r6hnlfcb

r6hnlfcb1#

约束布局中的android:paddingTop是问题的原因,删除它,您必须准备好。
尝试以下操作,

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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">

 <!-- child view remains the same -->

</androidx.constraintlayout.widget.ConstraintLayout>
hwamh0ep

hwamh0ep2#

另一种方法是,如果在根视图中没有这个paddingTop,则可能有一些

android:fitsSystemWindows="true"

在根视图中(你的导航主机所在的地方)。删除它并将它添加到你在NavHostFragment中注入的片段中。

相关问题