customized layout of SearchView
Recently, I use android.support.v7.widget.SearchView in my android project to provide a searchable ui. But, the default layout was not what I expected. I try to search for any ways to setup UI properties.
Unfortunately, the SearchView doesn’t provide any direct APIs to set up the properties what I need. So I have to try another way to solve that.
By reading the source code, I found an attribute named layout:

When the SearchView was created, it attempts to obtain a layout resouce as the root view. If the resource was not found, the default resource abc_search_view will be used. Cool, this indicates we can customize a layout xml and style the SearchView with it by adding attribute just like below:
<style name="SearView" parent="Widget.AppCompat.SearchView">
<item name="layout">@layout/your_customized_layout</item>
<!-- other attributes --></style>
Notice
in our customized layout, we should declare views with the ids below or we will get the NullPointerException:
search_buttonit’s anImageViewtype, clicked will show the editor and hide itself.search_edit_framecontainer of the content.search_mag_iconit’s anImageViewtype, indicates search button icon.search_plateseach plate, by default there are editor and close button inside.search_src_textthe editor, you can type some words on, it’s type ofSearchView.SearchAutoCompletesearch_close_buttonit’s type ofImageView, its clicked event will clear the focus of the editor and hide thesearch plateby default.submit_areacontainer of submit area.search_go_btngo/commit button, it’sImageViewtype.search_voice_btnvoice search button, it’sImageViewtype.
If you just wanna change some basic appearances, you can follow this attributes of SearchView.
If you want to learn more, you can read the source code.
I’m new of SearchView usage, with any questions you can discuss with me.