首页 > 解决方案 > 如何制作类似 Play 商店和 Gmail 的应用栏

问题描述

我想实现一个带有 Recyclerview 的应用栏,如下所示:


我尝试在 CoordinatorLayout 中使用 AppBarLayout,但应用栏位于 Recyclerview 上方


但是,我希望它像一个覆盖层,所以如果它是透明或有一些边距,recyclerview 内容如下所示:


我尝试将应用栏与 recyclerview 放在相对布局中,但是当我向下滚动时它不会隐藏(即使使用滚动标志也始终固定在顶部)。

这是我的 XML 代码:

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".fragment.MainFragment"
    android:background="@color/lightBackground">

    <RelativeLayout
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintBottom_toTopOf="@+id/bottom_navigation"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">
        <androidx.fragment.app.FragmentContainerView
            android:id="@+id/tab_fragment_container"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
        <androidx.coordinatorlayout.widget.CoordinatorLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">
            <com.google.android.material.appbar.AppBarLayout
                android:id="@+id/appBarLayout"
                android:layout_width="match_parent"
                android:layout_height="@dimen/main_action_bar_size"
                android:background="@color/white">
                <androidx.constraintlayout.widget.ConstraintLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:orientation="horizontal"
                    app:layout_scrollFlags="scroll|enterAlways|snap">
                    <FrameLayout
                        android:id="@+id/appBarDrawer"
                        android:layout_width="@dimen/main_action_bar_size"
                        android:layout_height="0dp"
                        app:layout_constraintBottom_toBottomOf="parent"
                        app:layout_constraintLeft_toLeftOf="parent"
                        app:layout_constraintTop_toTopOf="parent">
                        <ImageView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_gravity="center"
                            android:src="@drawable/ic_menu" />
                    </FrameLayout>
                    <FrameLayout
                        android:id="@+id/appBarSearch"
                        android:layout_width="0dp"
                        android:layout_height="0dp"
                        app:layout_constraintBottom_toBottomOf="parent"
                        app:layout_constraintLeft_toRightOf="@+id/appBarDrawer"
                        app:layout_constraintRight_toLeftOf="@+id/appBarVoiceSearch"
                        app:layout_constraintTop_toTopOf="parent">
                        <TextView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_gravity="center_vertical"
                            android:maxLines="2"
                            android:text="@string/search"
                            android:textColor="@color/lightGrayFont"
                            android:textSize="15sp" />
                    </FrameLayout>
                    <FrameLayout
                        android:id="@+id/appBarVoiceSearch"
                        android:layout_width="@dimen/main_action_bar_size"
                        android:layout_height="0dp"
                        app:layout_constraintBottom_toBottomOf="parent"
                        app:layout_constraintRight_toLeftOf="@+id/appBarMore"
                        app:layout_constraintTop_toTopOf="parent">
                        <ImageView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_gravity="center"
                            android:src="@drawable/ic_mic_none" />
                    </FrameLayout>
                    <FrameLayout
                        android:id="@+id/appBarMore"
                        android:layout_width="@dimen/main_action_bar_size"
                        android:layout_height="0dp"
                        app:layout_constraintBottom_toBottomOf="parent"
                        app:layout_constraintRight_toRightOf="parent"
                        app:layout_constraintTop_toTopOf="parent">
                        <ImageView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_gravity="center"
                            android:src="@drawable/ic_more" />
                    </FrameLayout>
                </androidx.constraintlayout.widget.ConstraintLayout>
            </com.google.android.material.appbar.AppBarLayout>

        </androidx.coordinatorlayout.widget.CoordinatorLayout>
    </RelativeLayout>


    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/bottom_navigation"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:menu="@menu/menu_bottom_navigation"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>



我还隐藏或显示它时要捕捉的内容,而不会像 Play Store 中的那样影响 recyclerview 滚动(似乎在不同的层中)。
在此处输入图像描述

标签: androidandroid-layoutmaterial-uimaterial-designandroid-appbarlayout

解决方案


如果你看,它实际上是一个被用作应用栏布局的卡片视图。像这样的东西应该让你得到你正在寻找的东西:

<android.support.design.widget.CoordinatorLayout
        android:id="@+id/main_content"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    <android.support.v7.widget.CardView
            android:layout_margin="16dp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">


    <android.support.design.widget.AppBarLayout
            android:id="@+id/appbar"

            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

        <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                app:layout_scrollFlags="scroll|enterAlways"
                app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>

    </android.support.design.widget.AppBarLayout>
    </android.support.v7.widget.CardView>

(对不起,我不记得我头顶上的 android x 库,供参考,这里是找到它们的链接https://developer.android.com/jetpack/androidx/migrate/artifact-mappings


推荐阅读