向下滚动时如何隐藏AppBar

时间:2020-05-29 13:37:27

标签: android android-studio android-layout

我的布局中有一个AppBar和其他东西。当用户向下滚动时,我希望AppBar(实际上是隐藏工具栏。这是我尝试过的,应用程序栏并未隐藏仅停留在其中的所有小部分。实际上,我认为问题出在我的标题屏幕上它会折叠起来,并在我滚动时添加一个小的空间。

enter image description here

enter image description here

正如您在第二张图片中看到的那样,某些部分停留了下来并没有隐藏。我需要在滚动时隐藏该部分。 请提供解决方案,我在做什么错了?

<?xml version="1.0" encoding="utf-8"?>

<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <androidx.coordinatorlayout.widget.CoordinatorLayout
        android:id="@+id/activity_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        android:orientation="vertical">

        <com.google.android.material.appbar.AppBarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/white">

            <com.google.android.material.appbar.CollapsingToolbarLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                app:contentScrim="?attr/colorPrimary"
                app:expandedTitleTextAppearance="@style/TextAppearance.AppCompat.Title"
                app:layout_scrollFlags="scroll|exitUntilCollapsed"
                app:titleEnabled="false">

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:gravity="center_vertical"
                    android:orientation="vertical">

                    <RelativeLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content">

                        <ImageView
                            android:id="@+id/slideIV"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:adjustViewBounds="true"
                            android:src="@drawable/ic_dish" />

                        <View
                            android:layout_width="match_parent"
                            android:layout_height="match_parent"
                            android:layout_above="@+id/bottomButtonRL"
                            android:layout_gravity="bottom"
                            android:background="@drawable/gradient_bg" />

                        <View style="@style/itemBottomViewLarge" />

                        <LinearLayout
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:layout_centerVertical="true"
                            android:layout_margin="@dimen/_20"
                            android:gravity="center_vertical"
                            android:orientation="vertical">

                            <de.hdodenhof.circleimageview.CircleImageView
                                android:layout_width="80dp"
                                android:layout_height="80dp"
                                android:src="@drawable/fake_chef_bg"
                                app:civ_border_color="@color/border_grey_color"
                                app:civ_border_width="@dimen/_2" />

                            <LinearLayout
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:layout_margin="@dimen/_5"
                                android:orientation="vertical">

                                <TextView
                                    android:layout_width="wrap_content"
                                    android:layout_height="wrap_content"
                                    android:fontFamily="@font/bold_font"
                                    android:text="@string/fatima_al_zahraa"
                                    android:textColor="@color/white"
                                    android:textSize="@dimen/large_text_size"
                                    android:textStyle="bold" />

                                <TextView
                                    android:layout_width="wrap_content"
                                    android:layout_height="match_parent"
                                    android:fontFamily="@font/regular_font"
                                    android:text="@string/presenter_of_the_cuient"
                                    android:textColor="@color/white"
                                    android:textSize="@dimen/app_text_size" />
                            </LinearLayout>
                        </LinearLayout>

                        <RelativeLayout
                            android:id="@+id/bottomButtonRL"
                            android:layout_width="match_parent"
                            android:layout_height="50dp"
                            android:layout_below="@+id/slideIV"
                            android:layout_marginTop="-25dp">

                            <LinearLayout
                                android:layout_width="match_parent"
                                android:layout_height="match_parent"
                                android:orientation="vertical">

                                <View
                                    android:layout_width="match_parent"
                                    android:layout_height="@dimen/_0"
                                    android:layout_weight="1" />

                                <View
                                    android:layout_width="match_parent"
                                    android:layout_height="@dimen/_0"
                                    android:layout_weight="1"
                                    android:background="@color/defaultColor" />
                            </LinearLayout>

                            <LinearLayout
                                android:layout_width="match_parent"
                                android:layout_height="wrap_content"
                                android:orientation="horizontal"
                                android:paddingStart="@dimen/_20"
                                android:paddingLeft="@dimen/_20">

                                <ImageView
                                    android:layout_width="wrap_content"
                                    android:layout_height="wrap_content"
                                    android:adjustViewBounds="true"
                                    android:src="@drawable/ic_share" />

                                <RelativeLayout
                                    android:layout_width="220dp"
                                    android:layout_height="wrap_content"
                                    android:layout_marginStart="@dimen/_10"
                                    android:layout_marginLeft="@dimen/_10"
                                    android:background="@drawable/ic_rec1">

                                    <TextView
                                        android:layout_width="match_parent"
                                        android:layout_height="match_parent"
                                        android:fontFamily="@font/regular_font"
                                        android:gravity="center"
                                        android:layout_centerInParent="true"
                                        android:text="@string/add_to_fav"
                                        android:textColor="@color/white"
                                        android:textSize="@dimen/medium_text_size" />
                                </RelativeLayout>

                            </LinearLayout>

                        </RelativeLayout>
                    </RelativeLayout>

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:background="@drawable/bottom_radius_view">

                        <TextView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:fontFamily="@font/regular_font"
                            android:padding="@dimen/_20"
                            android:text="@string/galetes_fires_au_plates_fires_au_plates_fires_au_plates_fires_au_plates_fires_au_plates"
                            android:textColor="@color/white"
                            android:textSize="@dimen/app_text_size" />
                    </LinearLayout>
                </LinearLayout>

                <androidx.appcompat.widget.Toolbar
                    android:visibility="gone"
                    android:id="@+id/toolbar"
                    android:layout_width="match_parent"
                    android:layout_height="?attr/actionBarSize"
                    app:layout_collapseMode="pin"
                    app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
                    />
            </com.google.android.material.appbar.CollapsingToolbarLayout>
        </com.google.android.material.appbar.AppBarLayout>

        <androidx.core.widget.NestedScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_behavior="@string/appbar_scrolling_view_behavior">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical"
                android:paddingLeft="@dimen/_20"
                android:paddingTop="@dimen/_10"
                android:paddingRight="@dimen/_20">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:fontFamily="@font/bold_font"
                    android:padding="@dimen/_5"
                    android:text="@string/les_videos"
                    android:textColor="@color/medium_grey_text_color"
                    android:textSize="@dimen/large_text_size" />

                <androidx.recyclerview.widget.RecyclerView
                    android:id="@+id/itemFRV"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_marginTop="@dimen/_5" />
            </LinearLayout>

        </androidx.core.widget.NestedScrollView>
    </androidx.coordinatorlayout.widget.CoordinatorLayout>

</layout>

这是标题代码:

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="@dimen/header_height"
        android:background="@color/white"
        android:gravity="center"
        android:orientation="horizontal"
        android:paddingLeft="@dimen/_20"
        android:paddingRight="@dimen/_20">

        <LinearLayout
            android:id="@+id/backLL"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_gravity="left"
            android:gravity="center"
            android:paddingRight="@dimen/_20"
            android:layout_marginBottom="@dimen/_10"
            android:visibility="gone">

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:adjustViewBounds="true"
                android:src="@drawable/ic_back" />
        </LinearLayout>

        <ImageView
            android:id="@+id/logoIV"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="left"
            android:adjustViewBounds="true"
            android:paddingStart="0dp"
            android:paddingTop="@dimen/_15"
            android:paddingEnd="@dimen/_10"
            android:layout_marginBottom="@dimen/_10"
            android:src="@drawable/ic_header_logo" />

        <View
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1" />

        <ImageView
            android:id="@+id/profileIV"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="right"
            android:adjustViewBounds="true"
            android:paddingStart="@dimen/_10"
            android:paddingTop="@dimen/_15"
            android:paddingEnd="0dp"
            android:layout_marginBottom="@dimen/_10"
            android:src="@drawable/ic_default_user" />
    </LinearLayout>
</layout>

2 个答案:

答案 0 :(得分:0)

更改layout_scrollFlags

app:layout_scrollFlags =“ scroll | exitUntilCollapsed”

在行上方替换为

app:layout_scrollFlags =“ scroll | enterAlways | enterAlwaysCollapsed”

答案 1 :(得分:0)

解决问题的方法就是删除

android:fitsSystemWindows="true"