简体   繁体   中英

Android SearchView open direction

I have successfully moved my menu items to the left side of the Toolbar.

But unfortunately when SearchView opens it opens toward left so it get disappeared while only the close button of SearchView is visible.

How should i force SearchView to open to the right?

Whether SearchView expands to left or right is not easily switchable, it is a consequence of what SearchView layout is.

You can, however, try overriding the layout of SearchView in your theme. I haven't done this myself, you may need to wiggle the layout a bit to get it right.

Derive your theme from Theme.Material and override the searchViewStyle attribute with your custom style for search view:

<style name="MyTheme" parent="android:Theme.Material">
    <item name="android:searchViewStyle">@style/MySearchView</item>
</style>

Then, derive the custom search view style and override the layout attribute:

<style name="MySearchView" parent="android:Widget.Material.SearchView">
    <item name="android:layout">@layout/search_view</item>
</style>

And finally, create your custom layout/search_view.xml to adjust the layout the way you want it. Copy over the original search_view.xml and make any changes you need -- probably change order of search_src_text ( line 79 ) and search_close_btn ( line 98 )

Don't forget to prefix all ids in the copied layout with android: (or not, if you're using the support lib variant, but that's another story, I'm sure you know what to do)


( Marcos Vasconcelos ) This solution doesn't works prior of Android 4.0.

( Ivan Bartsov ) Thanks for the edit, but it's not really true. Actually, prior to Android 5.0 (since it relies on Material theme, obviously) -- but it can be adjusted to be used with appcompat-v7 support library, which makes it suitable all the way down to 2.1. The OP obviously uses either Android 5.0+ or the support lib -- you may have noticed they're using Toolbar which is only available since API 21

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