Android无法添加搜索按钮,选项菜单 [英] Android can't add search button to options menu

查看:130
本文介绍了Android无法添加搜索按钮,选项菜单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我下面这个教程,但我不能得到一个简单的搜索按钮,出现选项菜单上。 code:

I am following this Tutorial but I can't get a simple search button to show up on the options menu. Code:

main_activity_actions.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android" >
    <!-- Search, should appear as action button -->
    <item android:id="@+id/action_search"
          android:icon="@drawable/ic_action_search"
          android:title="@string/action_search"
          android:showAsAction="ifRoom" />
    <!-- Settings, should always be in the overflow -->
    <item android:id="@+id/action_settings"
          android:title="@string/action_settings"
          android:showAsAction="never" /> </menu>

添加这MainActivity.java

public boolean onCreateOptionsMenu(Menu menu) {

    // Inflate the menu; this adds items to the action bar if it is present.
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.main_activity_actions, menu);
    return super.onCreateOptionsMenu(menu);

    }

应用程序加载行,但没有图标显示出来,我已经下载了标准图标,并把他们安置在每个可绘制文件夹。任何想法,为什么不工作?

App loads OK but no icons show up, I have downloaded standard icons and placed them in each of the drawable folders. Any ideas why is it not working?

不知道是否有关,但本教程的状态

Not sure if related but the tutorial states

的图标属性要求使用的图像资源ID。后面@绘制的名称/必须是你已经保存在项目的RES /绘制/目录中的位图图像的名称,例如,@绘制/ ic_action_search指ic_action_search.png。

"The icon attribute requires a resource ID for an image. The name that follows @drawable/ must be the name of a bitmap image you've saved in your project's res/drawable/ directory. For example, "@drawable/ic_action_search" refers to ic_action_search.png."

但我没有文件夹RES /绘制/只RES /绘制,华电国际,RES /绘制,LDPI ... 我Shold创建另一个文件夹RES /绘制/如果是哪个图标应该在它(官方图标集我下载了所有的RES /绘制,华电国际的人,但没有RES /绘制/)

But I didn't have folder res/drawable/ just res/drawable-hdpi, res/drawable-ldpi ... Shold I create another folder res/drawable/ if so which icon should go in it (the official icon set I downloaded had all the res/drawable-hdpi ones but no res/drawable/)

推荐答案

只需更换您的code与我,它会为你工作。

Just Replace your code with mine and it will work for you.

<menu xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        tools:context="com.example.actionbar.MainActivity" >
         <item android:id="@+id/action_search"
              android:icon="@drawable/ic_action_search"
              android:title="@string/action_search"
              app:showAsAction="always"/>


          <item android:id="@+id/action_settings"
              android:title="@string/action_settings"
              app:showAsAction="never"/>



    </menu>

这篇关于Android无法添加搜索按钮,选项菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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