使用 MenuItemCompat (Android) 获取 SearchView [英] Getting a SearchView with MenuItemCompat (Android)
问题描述
我正在尝试按照 android 开发人员所说的那样实现 SearchView ActionBar 项,但我遇到了一些问题.(http://developer.android.com/guide/topics/ui/actionbar.html).
I am trying to implement the SearchView ActionBar item as android developers says but I am having some trouble. (http://developer.android.com/guide/topics/ui/actionbar.html).
有两个错误,虽然我找了很多,还是没能找到解决办法.
There are two mistakes that although I have looked for a lot, I have not been able to find the solution.
1) 我对 MenuItemCompat 类有问题.它说:方法 getActionView(MenuItem) 未定义为 MenuItemCompat 类型
1) I have a problem with the class MenuItemCompat. It says: The method getActionView(MenuItem) is undefined for the type MenuItemCompat
我只能为这个类使用以下方法:
I can only use for this class the following methods:
- setShowAsAction(item, actionEnum)
- setActionView(item, view)
这是代码
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.restloader, menu);
MenuItem searchItem = menu.findItem(R.id.search_menu);
SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
// Configure the search info and add any event listeners
return super.onCreateOptionsMenu(menu);
}
2) 这个有问题:xmlns:myapp="http://schemas.android.com/apk/res-auto"我不明白为什么使用它,但如果谷歌说它,它一定是合适的.
2) There is a problem with this: xmlns:myapp="http://schemas.android.com/apk/res-auto" I don't understand why it is used but if google says it, it must be appropriate.
错误信息:在这一行找到多个注释:- 错误:在包中找不到属性actionViewClass"的资源标识符'com.example.pruebahttp3'- 错误:在包中找不到属性showAsAction"的资源标识符'com.example.pruebahttp3'
Error message: Multiple annotations found at this line: - error: No resource identifier found for attribute 'actionViewClass' in package 'com.example.pruebahttp3' - error: No resource identifier found for attribute 'showAsAction' in package 'com.example.pruebahttp3'
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:myapp="http://schemas.android.com/apk/res-auto" >
<item
android:id="@+id/search_menu"
android:orderInCategory="100"
android:title="@string/search"
android:icon="@drawable/ic_search_category_default"
myapp:showAsAction="ifRoom|collapseActionView"
myapp:actionViewClass="android.support.v7.widget.SearchView">
</item>
非常感谢!
推荐答案
我遇到了同样的问题,我使用以下代码解决了它.照顾好你的命名空间.`
i have got the same problem, i solved it by using the follow code. Be care of your namespace.`
<!-- Search, should appear as action button -->
<item
android:id="@+id/action_search"
android:icon="@drawable/abc_ic_search"
share:showAsAction="ifRoom"
share:actionViewClass="android.support.v7.widget.SearchView"
android:title="@string/abc_searchview_description_search" />
`
这篇关于使用 MenuItemCompat (Android) 获取 SearchView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!