Can't remove space on top of nav_host_fragment

Deadly 提交于 2020-08-22 09:21:27

问题


I just implemented a bottom navigation (AS's default - File -> New -> Activity -> Bottom Navigation Activity) Everything is fine except for a space on the top of the nav_host_fragment.

wrong space

Since it was generated in a ConstraintLayout, I tried to clean the constraints and set the top constraint with parent, setting margin to '0dp' and set height to match_constraint.

I unsuccessfully deleted the constraints and tried over and over again.

I used Clean Project.

I changed to RelativeLayout and set arguments like this:

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

But the space between nav_host_fragment and the top is still there.

Here's the lyout file:

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

回答1:


Remove this line from your Relative Layout.

android:paddingTop="?attr/actionBarSize"



回答2:


Answer from @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>



回答3:


in my layout i have also space bottom of navigation host fragment i count track out.

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


来源:https://stackoverflow.com/questions/57628238/cant-remove-space-on-top-of-nav-host-fragment

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!