获取与MenuItemCompat(安卓)一个搜索查看 [英] Getting a SearchView with MenuItemCompat (Android)

查看:200
本文介绍了获取与MenuItemCompat(安卓)一个搜索查看的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现的搜索查看动作条项目作为Android开发者说,但我有一些麻烦。 (<一href="http://developer.android.com/guide/topics/ui/actionbar.html">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(菜单项)是未定义的类型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(项目,actionEnum)
  • setActionView(项目,视图)

这是code

@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 =htt​​p://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>

非常感谢你!

推荐答案

我有同样的问题,我用后续的code解决它。要照顾你namespace.`的

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(安卓)一个搜索查看的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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