简体   繁体   中英

How to implement SearchView with Toolbar in Android?

I am trying to implement this toolbar which I have done in android studio, but I cannot figure out how to add a SearchView once the search icon is clicked.

未打开搜索的工具栏

This is what I want the toolbar to look like when the search icon is clicked.

带有搜索功能的工具栏

This is my toolbar.xml file:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:background="#000000"
    android:elevation="5dp"
    android:contentInsetLeft="15dp"
    >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Bean"
        android:layout_gravity="center"
        android:textColor="@color/beanBlue"
        android:id="@+id/toolbar_title"
        android:textSize="25sp"/>

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/cart_icon"
        android:id="@+id/cart_button"
        android:layout_gravity="right"
        android:layout_marginRight="15dp"/>

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/search_icon"
        android:id="@+id/search_button"
        android:layout_gravity="right"
        android:layout_marginRight="15dp"/>

</android.support.v7.widget.Toolbar>

How do I implement the toolbar with the SearchView included? Am I implementing the toolbar wrong?

try this library. this can solve your problem

https://github.com/leonardoxh/FakeSearchView

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM