使用 support-v7-appcompat 在 ActionBar 中搜索视图

2024-02-27

我一直在努力使用 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)

或者,我在没有 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

我希望在使用 appcompat 库时第二张图片中的 searchview 可用,但由于某种原因它似乎不起作用。我正在使用 eclipse,并且我已经包含了支持库,其资源与中指定的完全一样支持库设置[developer.android.com] https://developer.android.com/tools/support-library/setup.html#libs-with-res.

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

我怀疑支持库设置有问题,有人可以告诉我我可能做错了什么吗?谢谢!


Maybe SearchView未显示,因为您错过了添加collapseActionView在这一行中:yourapp:showAsAction="always".

此外,您的活动必须延长AppCompatActivity。所以,添加应用程序兼容性 http://developer.android.com/tools/support-library/features.html#v7-appcompat库到项目

您可以阅读更多详细信息link http://developer.android.com/guide/topics/ui/actionbar.html#ActionView

希望它能帮助你。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用 support-v7-appcompat 在 ActionBar 中搜索视图 的相关文章

随机推荐