【问题标题】:How to make an app bar like Play store and Gmail如何制作类似 Play 商店和 Gmail 的应用栏
【发布时间】:2021-01-17 16:46:21
【问题描述】:

我想实现一个带有 Recyclerview 的应用栏,例如 Gmail 和 Play 商店中的应用栏,如下所示:


我尝试在 CoordinatorLayout 内使用 AppBarLayout,但 App Bar 位于 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 商店中那样的 recyclerview 滚动(似乎在一个不同的层中)。

【问题讨论】:

    标签: android android-layout material-ui material-design android-appbarlayout


    【解决方案1】:

    如果你看,它实际上是一个被用作应用栏布局的卡片视图。像这样的东西应该可以为您提供所需的内容:

    <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

    【讨论】:

    • 我之前试过这个,问题是如果cardview是透明的或者有margin的,recyclerview的内容不会显示在下面
    猜你喜欢
    • 1970-01-01
    • 2016-04-05
    • 1970-01-01
    • 1970-01-01
    • 2023-03-28
    • 2014-06-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多