简体   繁体   中英

Prevent AppBarLayout.ScrollingViewBehavior from adding top margin to the view?

I'm trying to achieve a layout where my view will extend all the way across the screen, under the ActionBar , and the ActionBar will be transparent. I also need the ActionBar and BottomNavigationView to hide on scroll, so I'm using CoordinatorLayout .

I'm using the AppCompat theme with the attribute <item name="windowActionBarOverlay">true</item> in order to have my views extend under the ActionBar. However, after adding app:layout_behavior="@string/appbar_scrolling_view_behavior" to my container Fragment, the view gets placed below the ActionBar.

Is there a way to prevent this part of the appbar_scrolling_behaviour ? I've tried without the behaviour, but the scrolling of the AppBar is not synced witht he RecyclerView scrolling underneath it. Thank you!

Here's a gif of the problem

PS: I'm trying to use the Android Navigation Component, so I'm following a single activity, multi fragment architecture, if that matters.

Try this code and make some changes because i used androidx.. make main layout like this way..

<?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:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white">


<com.google.android.material.appbar.AppBarLayout
    android:id="@+id/appBarLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    >

    <androidx.appcompat.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="@color/white"
        android:theme="@style/MyToolbar"
        app:contentInsetStart="0dp"
        app:contentInsetStartWithNavigation="0dp"
        app:titleTextAppearance="@style/MyToolbar">

        <ImageView
            android:id="@+id/headerIcon"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="@dimen/_6sdp"
            android:src="@drawable/ic_edit_location_black"
            android:visibility="gone" />

        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="@dimen/_6sdp">

            <TextView
                android:id="@+id/header"
                style="@style/MyToolbar"
                android:layout_width="wrap_content"
                android:layout_height="?attr/actionBarSize"
                android:gravity="center_vertical"
                android:textColor="@color/primary"
                app:layout_constraintStart_toStartOf="parent" />

            <ImageView
                android:id="@+id/ivSearch"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginEnd="@dimen/_10sdp"
                android:src="@drawable/search_icon"
                android:visibility="gone"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintTop_toTopOf="parent" />

        </androidx.constraintlayout.widget.ConstraintLayout>

    </androidx.appcompat.widget.Toolbar>


</com.google.android.material.appbar.AppBarLayout>

<FrameLayout
    android:id="@+id/activity_main_container"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    app:layout_constraintBottom_toTopOf="@+id/bottomContainer"
    app:layout_constraintTop_toBottomOf="@+id/appBarLayout" />


    <RelativeLayout
       android:id="@+id/bottomContainer"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">


        <com.google.android.material.bottomnavigation.BottomNavigationView
            android:id="@+id/bvTabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:theme="@style/Widget.BottomNavigationView"
            app:itemIconTint="@drawable/navigation_drawable_tint"
            app:itemTextColor="@drawable/navigation_drawable_tint"
            app:menu="@menu/bottom_menu_item" />


    </RelativeLayout>

 </androidx.constraintlayout.widget.ConstraintLayout>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM