Android 带抽屉的 Google 搜索栏

Mer*_*aju 6 android kotlin android-search android-drawer android-architecture-navigation

我的应用程序目前是使用单一活动方法实现的(使用具有一个主要活动和多个片段的导航架构组件)。我目前正在使用带抽屉的工具栏。

我的应用程序目前看起来像这样:

在此输入图像描述

然而,在现代谷歌应用程序(谷歌照片、gmail 等)中,谷歌实现了一种使用搜索字段进行导航的新方法,其中包含一个已实现的抽屉,如下所示:

在此输入图像描述

我想用搜索栏和抽屉菜单替换这个工具栏,就像 Google 应用程序一样。

有人可以帮助我编写一些关于如何实现此目标的代码吗?

我的主要活动如下:

<?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"
    xmlns:tools="http://schemas.android.com/tools">

<androidx.drawerlayout.widget.DrawerLayout
    android:id="@+id/Drawer_Main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".ui.main.main.MainActivity">

    <androidx.coordinatorlayout.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true">

        <com.google.android.material.appbar.MaterialToolbar
            android:id="@+id/Toolbar_Main"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary">

            <TextView
                android:id="@+id/Toolbar_Title"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
            android:text="@string/app_name"
                style="@style/Locky.Toolbar.TitleText" />

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

        <androidx.core.widget.NestedScrollView
            android:id="@+id/Nested_Scroll"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="?attr/actionBarSize"
            app:layout_behavior="@string/appbar_scrolling_view_behavior">

            <fragment
                android:id="@+id/Navigation_Host"
                android:name="androidx.navigation.fragment.NavHostFragment"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                app:defaultNavHost="true"
                app:navGraph="@navigation/navigation_drawer_main" />

        </androidx.core.widget.NestedScrollView>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:orientation="vertical">

        <com.google.android.material.floatingactionbutton.FloatingActionButton
            android:id="@+id/FAB_Account"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="10dp"
            android:layout_marginEnd="16dp"
            android:layout_marginStart="16dp"
            android:visibility="invisible"
            app:srcCompat="@drawable/ic_account"
            style="@style/Locky.FloatingActionButton.Mini" />

        <com.google.android.material.floatingactionbutton.FloatingActionButton
            android:id="@+id/FAB_Card"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="10dp"
            android:layout_marginEnd="16dp"
            android:layout_marginStart="16dp"
            android:visibility="invisible"
            app:srcCompat="@drawable/ic_credit_card"
            style="@style/Locky.FloatingActionButton.Mini" />

        <com.google.android.material.floatingactionbutton.FloatingActionButton
            android:id="@+id/FAB_Add"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="@dimen/fab_margin"
            android:layout_marginEnd="@dimen/fab_margin"
            android:layout_marginBottom="@dimen/fab_margin"
            app:srcCompat="@drawable/ic_add"
            style="@style/Locky.FloatingActionButton.Normal"/>

    </LinearLayout>

</androidx.coordinatorlayout.widget.CoordinatorLayout>

    <com.google.android.material.navigation.NavigationView
        android:id="@+id/Navigation_View"
        android:layout_width="280dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:clipToPadding="false"
        app:itemTextAppearance="@style/Locky.TextAppearance.Drawer.Item"
        app:menu="@menu/menu_drawer_main"
        app:headerLayout="@layout/drawer_header"
        style="@style/Locky.Widget.Custom.NavigationView" />

</androidx.drawerlayout.widget.DrawerLayout>

</layout>
Run Code Online (Sandbox Code Playgroud)

有人可以指导我一些关于如何实现这种搜索栏的代码吗?

小智 2

我无法告诉您如何设计该工具栏,但这个项目也许可以帮助您使用抽屉布局处理工具栏的后退按钮和主页按钮。

https://github.com/furkanaskin/Weatherapp/blob/master/app/src/main/java/com/faskn/app/weatherapp/ui/main/MainActivity.kt