带有图标的android选项菜单 [英] android option menu with icon

查看:100
本文介绍了带有图标的android选项菜单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何显示带有选项菜单的图标.我尝试了以下代码,但我的选项菜单没有图像图标.我正在使用Android 4.0版开发应用.

How to show icon with option menu.I have tried the following code but my option menu is without image icon.I am using android version 4.0 for developing app.

Java代码:

 public boolean onCreateOptionsMenu(Menu menu) {
            super.onCreateOptionsMenu(menu);
             menu.add("Add Contacts").setIcon(
                    R.drawable.ic_launcher);

            return true;
        }

以下是我的应用程序的屏幕截图

Following is my app's screen shot

我需要在添加联系人"项目的顶部显示图片.

I need image to be displayed on the top of "Add Contacts" item.

推荐答案

我尝试了两行代码,并且有效:

I tried the code in two line and it works:

public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);
        menu.add("Add Contacts");
        menu.getItem(0).setIcon(R.drawable.ic_launcher);
        return true;
}

这篇关于带有图标的android选项菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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