eclipse 使用 support-v7-appcompat 在 ActionBar 中搜索视图
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21265690/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
SearchView in ActionBar using support-v7-appcompat
提问by phininity
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:
我一直在努力使用 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.
这甚至没有显示搜索按钮,更不用说在点击时展开它了。它只是将搜索添加到菜单中,而不是在操作栏中显示。
Alternatively I tried the same without the appcompat library , I simply replaced the menu.xml with:
或者,我在没有 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.
它工作得非常好,甚至可以在单击时扩展到搜索文本输入小部件。
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].
我希望在使用 appcompat 库时第二张图片中的搜索视图可用,但由于某种原因它似乎不起作用。我正在使用 eclipse,并且我已经包含了支持库,其中的资源与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.
我的清单文件的 minsdk 版本为 7,targetsdk 版本为 18,构建目标也是 18。
I suspect something is amiss in the support library setup, can someone please tell me what I might be doing wrong? Thanks!
我怀疑支持库设置有问题,有人可以告诉我我可能做错了什么吗?谢谢!
回答by Volodymyr Yatsykiv
Maybe SearchView
wasn't showed because you missed to add a collapseActionView
in this line: yourapp:showAsAction="always"
.
可能SearchView
没有显示,因为您错过了collapseActionView
在此行中添加 a : yourapp:showAsAction="always"
。
Also, your activity must extends AppCompatActivity
. So, add AppCompatlibrary to project
此外,您的活动必须扩展AppCompatActivity
. 因此,将AppCompat库添加到项目中
More details you can read on this link
您可以在此链接上阅读更多详细信息
Hope it will help you.
希望它会帮助你。