【问题标题】:Can't remove space on top of nav_host_fragment无法删除 nav_host_fragment 顶部的空间
【发布时间】:2019-12-28 21:27:02
【问题描述】:

我刚刚实现了一个底部导航(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和顶部之间的空间还在。

这是 lyout 文件:

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

【问题讨论】:

  • 这看起来像&lt;RelativeLayout&gt; 上的android:paddingTop="?attr/actionBarSize"
  • 您使用的是什么主题?也许是为“ActionBar”保留空间?
  • @MikeM。这正是造成空间的原因。我不敢相信我没有注意到这一点!谢谢!
  • 您可以将此评论作为答案发布,以便我标记为适合此问题的评论。
  • 没问题。我很好。 :-) 没什么大不了的。只是一个小小的疏忽。如果您愿意,请随时自行发布答案,或删除此问题。不过谢谢。我很感激这个提议。干杯!

标签: android android-fragments bottomnavigationview


【解决方案1】:
从您的相对布局

删除这一行。

android:paddingTop="?attr/actionBarSize"

【讨论】:

  • 在 2021 年,Android Studio 中默认的“底部导航活动”仍然出于某种原因在底部导航视图的父布局中添加了一个 paddingTop。
【解决方案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>

【讨论】:

    【解决方案3】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-10-25
      • 2021-05-14
      • 1970-01-01
      • 2021-12-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多