我正在尝试在 Android 上创建此搜索栏

I'm trying to create this Search bar on Android

我正在尝试在 Android 上创建此搜索栏,其中左侧的放大镜是固定的,搜索框的背景是白色的圆角,左侧和背面之间的空格按钮、右侧、搜索框、右侧为 16dp。什么是更好的方法?

我尝试使用 menuitem,但我并不走运。

Android 有一个现成的搜索框,如果您导入 android.widget.SearchView 就可以使用它。它具有您要求的大部分(如果不是全部)功能并且已经完成。你只需要将它添加到你的布局中。还有一些方法会在有人在搜索框中键入内容或单击放大镜时触发。如果需要,您可以隐藏放大镜 and/or X 图标,或者像示例中那样显示它们。您最好使用标准 Android SearchView,而不是自己编写,因为这样一来,它就符合每个用户都已经熟悉的标准 UI 准则。

您可以通过在 .xml 布局文件中添加填充来调整间距。

我找到的解决方案是将 SearchView 放入 toolbar:

<android.support.v7.widget.Toolbar
    android:id="@+id/restaurant_search_menu_toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="?attr/colorPrimary"
    android:elevation="4dp"
    android:theme="@style/MYSTYLE"
    app:contentInsetStartWithNavigation="0dp"
    app:contentInsetStart="0dp"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
 android:subtitleTextAppearance="@style/MYSTYLE.SubTitle.TextAppearance">

    <SearchView
        android:id="@+id/menu_search_view"
        android:layout_width="match_parent"
        android:layout_height="36dp"
        android:layout_marginEnd="16dp"
        android:layout_marginBottom="10dp"
        android:layout_marginTop="10dp"
        android:imeOptions="actionDone"
        android:closeIcon="@drawable/ic_dls_glyph_close"
        android:searchIcon="@drawable/ic_dls_icon_search_filled"
        android:background="@drawable/white_background_rounded"
        android:iconifiedByDefault="false"
        tools:queryHint="Search for a menu item"/>

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