SearchView in ActionBar using support-v7-appcompat

喜欢而已 提交于 2019-12-23 07:31:10

问题


I've been trying very hard to get the SearchView widget to expand in the actionbar using the support-v7 libraries. I've managed to get it working without the support libraries when I target 4.0+ but I want to write the app for 2.3+ so I need to use the support libraries. I created a blank new activity with the following menu.xml:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:yourapp="http://schemas.android.com/apk/res-auto" >

<item
    android:id="@+id/action_settings"
    android:orderInCategory="100"
    android:showAsAction="never"
    android:title="@string/action_settings"/>
    
<item
    android:id="@+id/action_search"
    android:icon="@android:drawable/ic_menu_search"
    yourapp:showAsAction="always"
    yourapp:actionViewClass="android.support.v7.widget.SearchView"
    android:title="Search"/>
</menu>

This does not even show the search button, let alone expand it upon clicking. It simply add's the search into the menu instead of showing it in the actionbar.

Alternatively I tried the same without the appcompat library , I simply replaced the menu.xml with:

<item
    android:id="@+id/action_search"
    android:icon="@android:drawable/ic_menu_search"
    android:showAsAction="always"
    android:actionViewClass="android.widget.SearchView"
    android:title="Search"/>

And it works perfectly fine, and even expands to the search text input widget upon clicking.

I want the searchview as available in the second picture while using the appcompat library, but for some reason it doesn't seem to be working. I'm using eclipse and I've included the Support libraries with resources exactly as specified in Support Library Setup[developer.android.com].

My manifest file has minsdk version as 7, targetsdk version as 18, and the build target is also 18.

I suspect something is amiss in the support library setup, can someone please tell me what I might be doing wrong? Thanks!


回答1:


Maybe SearchView wasn't showed because you missed to add a collapseActionView in this line: yourapp:showAsAction="always".

Also, your activity must extends AppCompatActivity. So, add AppCompat library to project

More details you can read on this link

Hope it will help you.



来源:https://stackoverflow.com/questions/21265690/searchview-in-actionbar-using-support-v7-appcompat

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!