SearchView在ActionBar中使用support-v7-appcompat [英] SearchView in ActionBar using support-v7-appcompat

查看:156
本文介绍了SearchView在ActionBar中使用support-v7-appcompat的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在努力让SearchView小部件在actionbar中使用support-v7库进行扩展。我设法使其工作没有支持库,当我目标4.0+,但我想编写的应用程序2.3+,所以我需要使用支持库。
我使用以下menu.xml创建了一个空白的新活动:

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.

或者,我没有使用appcompat库,尝试一下,我只是用以下方式替换menu.xml:

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.

我希望在使用appcompat库时,第二张照片中的搜索视图可用,但由于某种原因,它似乎没有起作用。我正在使用eclipse,我已经在支持库中包含了完全按照支持库设置[developer.android.com]

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].

我的清单文件的minsdk版本为7,targetsdk版本为18 ,并且构建目标也是18。

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!

推荐答案

也许 SearchView 没有显示,因为你错过了在这一行中添加一个 collapseActionView yourapp:showAsAction =always

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

此外,您的活动必须扩展 AppCompatActivity 。因此,将 AppCompat 库添加到项目

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

更多细节可以阅读这个链接

More details you can read on this link

希望会帮助你。

这篇关于SearchView在ActionBar中使用support-v7-appcompat的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆