菜单图标未显示在操作栏中 [英] Menu icon is not displaying in action bar

查看:79
本文介绍了菜单图标未显示在操作栏中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在Fragment中添加自定义菜单.

I need to inflate custom menu in Fragment.

我只有一个菜单项.但是图标没有显示.

I have only one menu item.But the icon is not displaying.

有人可以告诉我我的代码有什么问题

Can someone tell what is wrong with my code

我的menu.xml

My menu.xml

<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"  >

<item
    android:id="@+id/search"
    android:icon="@android:drawable/ic_search_category_default"
    app:showAsAction="always"
    android:title="Search"/></menu>

然后我在onCreateView()中设置

And I set in onCreateView()

    setHasOptionsMenu(true);
    getActivity().invalidateOptionsMenu();

并扩大菜单

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    // TODO Auto-generated method stub

    inflater = getActivity().getMenuInflater();

    inflater.inflate(R.menu.menu, menu);

}

结果屏幕如下所示.我需要有搜索图标,而不是菜单溢出图标.

Resulting screen attached below. I need to have the search icon instead of the menu overflow icon.

推荐答案

我知道我参加聚会有点晚了,但希望我能帮助别人.今天,我面临着同样的问题.

I know I'm a little late for the party, but hope I can help someone else. Today I was facing this SAME problem.

我使用android:showAsAction ="always"代替了app:showAsAction ="always"

I fixed using android:showAsAction="always" instead of app:showAsAction="always"

<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">

<item android:id="@+id/bluetooth_status_off"
    android:orderInCategory="0"
    android:icon="@drawable/bluetooth_red"
    android:title="@string/app_name"
    android:showAsAction="always" />
</menu>

在showAsAction上用红色下划线(警告),但是效果很好.

on the showAsAction is underlined with red (warning), but works fine.

这篇关于菜单图标未显示在操作栏中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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