-->

Fragment Content Overlaps Toolbar and Bottom Navig

2020-08-25 04:59发布

问题:

I have implemented a bottom navigation view in my android application. With five fragments. However, I have a problem that each time I change the fragment the content of the fragment overlaps the toolbar.

layout for the bottom navigation view

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".DashboardActivity">

    <include layout="@layout/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    </include>

    <FrameLayout
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_above="@+id/navigationView"
        android:layout_height="match_parent"


       />

    <android.support.design.widget.BottomNavigationView
        android:id="@+id/navigationView"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginEnd="0dp"
        android:layout_marginStart="0dp"
        android:background="?android:attr/windowBackground"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:itemBackground="@android:color/white"
        app:itemIconTint="@color/cardview_dark_background"
        app:itemTextColor="@android:color/black"
        app:menu="@menu/navigation_menu"
        />
</android.support.constraint.ConstraintLayout>

an example of one of the fragments layout

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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/frameLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".AttendanceFragment">

    <android.support.constraint.Guideline
        android:id="@+id/guideline"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintGuide_percent="0.7" />

    <android.support.v7.widget.CardView
        android:id="@+id/cardView2"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toTopOf="@+id/barrier3"
        app:layout_constraintEnd_toStartOf="@+id/guideline"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <Spinner
            android:id="@+id/spinner_courses"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="8dp"
            android:paddingBottom="4dp"
            android:paddingTop="4dp"
            app:layout_constraintEnd_toStartOf="@+id/guideline"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    </android.support.v7.widget.CardView>

    <TextView
        android:id="@+id/tv_time"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:textColor="@color/colorPrimaryDark"
        android:textSize="12sp"
        app:layout_constraintBottom_toTopOf="@+id/barrier3"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="@+id/guideline"
        app:layout_constraintTop_toTopOf="parent"
        tools:text="Date" />


    <android.support.design.widget.FloatingActionButton
        android:id="@+id/add_fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|right"

        android:layout_margin="16dp"

        android:src="@drawable/ic_add_icon"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintRight_toRightOf="parent"/>

    <!--<android.support.design.widget.FloatingActionButton
        android:id="@+id/floatingActionButton"
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:layout_marginBottom="4dp"
        android:layout_marginEnd="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="4dp"
        android:clickable="true"
        app:srcCompat="@drawable/ic_more_vert_black_24dp"
        app:fabSize="mini"
        app:layout_constraintBottom_toBottomOf="@+id/textView3"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/spinner"
        app:layout_constraintTop_toTopOf="parent"
        android:focusable="true" />-->

    <android.support.constraint.Barrier
        android:id="@+id/barrier3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:barrierDirection="bottom"
        app:constraint_referenced_ids="cardView2,tv_time"
        tools:layout_editor_absoluteY="511dp" />

    <android.support.v7.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_marginTop="4dp"
        app:cardBackgroundColor="@color/colorMainBackground"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@+id/barrier3">

        <android.support.v7.widget.RecyclerView
            android:id="@+id/rv_register"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

        </android.support.v7.widget.RecyclerView>
    </android.support.v7.widget.CardView>

</android.support.constraint.ConstraintLayout>

This is what the screen look like with overlapping content

回答1:

Add margins to container:

<FrameLayout
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_above="@+id/navigationView"
    android:layout_height="match_parent"
    android:layout_marginBottom="{bottom_navigation_height}"
    android:layout_marginTop="{tool_bar_height}"

   />


回答2:

I removed from my ConstraintLayout the default generated marginTop field, and changed my fragment's layout_height to 0dp. Then in all of my Fragments' layout's the outermost layout_height I always set to match parent.

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

    <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="0dp"
        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" />

</androidx.constraintlayout.widget.ConstraintLayout>


回答3:

Its overlapping because You have not giving proper constraint to your controls.

You can try this way.

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    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:layout_width="match_parent"
    android:layout_height="match_parent">

    <include
        android:id="@+id/include"
        layout="@layout/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>

    <FrameLayout
        android:id="@+id/container"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintBottom_toTopOf="@+id/navigationView"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/include" />

    <android.support.design.widget.BottomNavigationView
        android:id="@+id/navigationView"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginEnd="0dp"
        android:layout_marginStart="0dp"
        android:background="?android:attr/windowBackground"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:itemBackground="@android:color/white"
        app:itemIconTint="@color/cardview_dark_background"
        app:itemTextColor="@android:color/black"
        app:menu="@menu/navigation_menu"
        />
</android.support.constraint.ConstraintLayout>


回答4:

You should add constraints to your @id/container. To the top to your toolbar and to the bottom to the BottomNavigationBar. The same should you do to these elements. Toolbar top to parent and toolbar bottom to container. BottomNavigationBar top to container and bottom to parent. Then set the height of the container to 0dp.

<include layout="@layout/toolbar"
    android:id="@+id/toolbar"
    android:layout_width="0dp"
    android:layout_height="?attr/actionBarSize"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"/>

<FrameLayout
    android:id="@+id/container"
    android:layout_width="0dp"
    android:layout_height="0dp"
    app:layout_constraintBottom_toBottomOf="@+id/navigationView"
    app:layout_constraintTop_toTopOf="@id/toolbar"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"/>

<android.support.design.widget.BottomNavigationView
    android:id="@+id/navigationView"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"/>

Didn't test the code but should work.



回答5:

In Recent Android Studio Versions (4.0.beta 1), if the hierarchy on the default generated activity_main.xml (omitted attributes) is as following,

<androidx.constraintlayout.widget.ConstraintLayout>
    <com.google.android.material.bottomnavigation.BottomNavigationView />
    <fragment />
</androidx.constraintlayout.widget.ConstraintLayout>

The fragments seems overlapping the bottom navigation, just rearranging it as shown below,

<androidx.constraintlayout.widget.ConstraintLayout>
    <fragment />
    <com.google.android.material.bottomnavigation.BottomNavigationView />
</androidx.constraintlayout.widget.ConstraintLayout>

fixed the problem for me without touching any constraints and NOT adding any padding or margins



回答6:

your fragment container used "match_parent",which take up the position of toolbar and navigationBar,maybe you can choice nested LinearLayout and use height=warp_content, weight=1