带有抽屉的Android Google搜索栏 [英] Android Google Search Bar with Drawer

查看:85
本文介绍了带有抽屉的Android Google搜索栏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用当前通过单一活动方法实现(使用具有一个主要活动和多个片段的导航体系结构组件).我目前正在使用带抽屉的工具栏.

My app is currently implemented with the Single activity approach (Using navigation architecture component with one main activity and several fragments). I am currently using a toolbar with a drawer.

我的应用当前如下所示:

My app currently look like this:

然而,在现代的google应用(Google照片,gmail等)中,Google实现了一种新的导航方式,即使用其中包含已实现的抽屉的搜索字段进行导航,如下所示:

However in the modern google apps (Google photos, gmail etc..), Google has implemented a new way of navigating using a search field with an implemented drawer in it as shown below:

我想将此搜索栏和抽屉菜单完全替换为Google工具栏上的工具栏.

I want to replace this toolbar with a search bar and the drawer menu exactly like the Google apps.

有人可以通过一些代码帮助我实现这一目标吗?

Can someone help me with some code on how to achieve this?

我的主要活动如下:

<?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>

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

Can someone please guide me with some code on how to implement this kind of search bar?

推荐答案

如果您仍在寻找通过应用抽屉图标实现gmail样式搜索栏的方法,请使用工具栏,只需添加导航图标并包装editText内部:

if you are still looking for a way to implement a gmail style search bar with an app drawer icon, use the toolbar, simply add navigation icon and wrap an editText inside:

<androidx.appcompat.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    app:navigationIcon="@drawable/ic_menu">
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
</androidx.appcompat.widget.Toolbar>

然后在活动中设置工具栏:

Then set the toolbar in the activity:

val toolbar: Toolbar = findViewById(R.id.toolbar)
setSupportActionBar(toolbar)

并实现onOptionsItemSelect侦听器:

And implement the onOptionsItemSelect listener:

override fun onOptionsItemSelected(item: MenuItem): Boolean {
    // do desired stuff here
    return super.onOptionsItemSelected(item)
}

如果您还想拥有浮动样式和消失的行为,请将工具栏包装在appBarLayout中,然后设置app:layout_scrollFlags ="scroll | enterAlways | snap":

If you also want to have the floating style and disappearing behavior, wrap the toolbar in an appBarLayout and set app:layout_scrollFlags="scroll|enterAlways|snap":

<com.google.android.material.appbar.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginHorizontal="24dp"
    android:layout_marginTop="2dp"
    android:background="@android:color/transparent">
    app:elevation="0dp">

    <androidx.appcompat.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:elevation="4dp"
        app:navigationIcon="@drawable/ic_menu"
        app:contentInsetStartWithNavigation="0dp"
        app:layout_scrollFlags="scroll|enterAlways|snap">

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@android:color/transparent"/>
</androidx.appcompat.widget.Toolbar>

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

以下模仿样式和行为的细节值得一提:

The following details to mimic the style and the behavior are worth to mention:

appBarLayout

appBarLayout

  1. 水平边距,以使栏不覆盖整个顶部
  2. 背景设置为圆形矩形
  3. 边距顶端要留出一点空间
  4. 提升到0dp,因此appBarLayout没有阴影

工具栏

  1. app:contentInsetStartWithNavigation ="0dp",否则图标和editText之间的边距太大

EditText

  1. 背景透明,因此下划线将消失

edit:几乎忘了!非常重要的一点:父级布局必须为CoordinatorLayout,否则在recyclerview中滚动时不会有任何反应edit2:将应用栏布局的高度更改为0dp,因此不会有阴影

edit: Almost forgot! Very important point: The parent layout has to be CoordinatorLayout, otherwise there will be no reactions to scrolling inside the recyclerview edit2: changed eleveation of the appbar layout to 0dp so there wont be any shadow

这篇关于带有抽屉的Android Google搜索栏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆