如何实现自定义 SearchView 布局?

cjo*_*724 6 android searchview

我正在尝试实现自定义 SearchView 布局。我已经成功更改了 android.support.v7.appcompat.SearchView 的许多属性,例如背景颜色和文本颜色,但我还想自定义更多。所以我制作了自己的 search_view_layout.xml 文件,但我不知道如何实现它。这是 search_view_layout.xml 文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="horizontal"
              android:layout_gravity="top"
              android:background="#FFFFFF"
              android:layout_width="match_parent"
              android:layout_height="40dp"
              android:paddingLeft="5dp"
              android:paddingTop="5dp"
              android:paddingRight="5dp"
              android:paddingBottom="5dp"
              android:padding="5dp">

    <SearchView
        android:id="@+id/leftMenuSearch"
        android:background="#FFFFFF"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:queryHint="What are you looking for?"
        android:layout_weight="1"
        android:layout_marginEnd="5dp"
        android:layout_marginRight="5dp"
        android:maxWidth="1000dp"/>

    <Button
        android:id="@+id/cancel_button"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="4"
        android:background="@color/beanBlue"
        android:text="Cancel"
        android:textColor="#FFFFFF"/>

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

这是我的 menu_main.xml 文件:

<?xml version="1.0" encoding="utf-8"?>
<menu 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"
      xmlns:appcompat="http://schemas.android.com/apk/res-auto">
    <item android:id="@+id/action_search"
          android:title="Search"
          android:icon="@drawable/search_icon"
          app:showAsAction="always|collapseActionView"
          app:actionLayout="@layout/search_view_layout"
          app:actionViewClass="android.support.v7.widget.SearchView"/>

    <item android:id="@+id/action_cart"
          android:title="Cart"
          android:icon="@drawable/cart_icon"
          app:showAsAction="ifRoom"/>
</menu>
Run Code Online (Sandbox Code Playgroud)

这是我的 toolbar.xml 文件:

<?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"
    app:collapseIcon="@drawable/collapse_back_button">

    <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"/>


</android.support.v7.widget.Toolbar>
Run Code Online (Sandbox Code Playgroud)

这是按下搜索图标之前工具栏的样子:

搜索前的工具栏

这就是我希望 SearchView 在按下搜索图标时的样子:

带有 SearchView 的工具栏

如何在我的 android 项目中实现这个自定义 SearchView 布局?

Yu *_*Lee 1

final int layoutResId = a.getResourceId(R.styleable.SearchView_layout, R.layout.abc_search_view);
inflater.inflate(layoutResId, this, true);
Run Code Online (Sandbox Code Playgroud)

我在 中找到了上面的代码android.support.v7.widget.SearchView.java,您可以在 xml 中定义自定义布局,然后只需添加app:layout="@layout/resId".

注意

您必须阅读源代码才能为所有视图提供相同的 id,否则它将因NullPointerException. 详细的可以看源码SearchView(Context context, AttributeSet attrs, int defStyleAttr)