我一直在努力使用 support-v7 库让 SearchView 小部件在操作栏中展开。当我的目标是 4.0+ 时,我已经设法在没有支持库的情况下让它工作,但我想为 2.3+ 编写应用程序,所以我需要使用支持库。
我使用以下 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.
![using appcompat library (does not work)](https://i.stack.imgur.com/i53X3.jpg)
或者,我在没有 appcompat 库的情况下尝试了相同的操作,我只是将 menu.xml 替换为:
<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.
![enter image description here](https://i.stack.imgur.com/zUa2J.jpg)
我希望在使用 appcompat 库时第二张图片中的 searchview 可用,但由于某种原因它似乎不起作用。我正在使用 eclipse,并且我已经包含了支持库,其资源与中指定的完全一样支持库设置[developer.android.com] https://developer.android.com/tools/support-library/setup.html#libs-with-res.
我的清单文件中 minsdk 版本为 7,targetsdk 版本为 18,构建目标也是 18。
我怀疑支持库设置有问题,有人可以告诉我我可能做错了什么吗?谢谢!