Android 选项菜单未显示 [英] Android options menu not displaying

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

问题描述

我是 Android 新手,我一直在尝试添加一个简单的添加按钮,如下所述

I'm new to Android and I've been trying to add a simple add button as mentioned below

list_menu.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item 
        android:id="@+id/menu_insert"
        android:icon="@android:drawable/ic_menu_add"
        android:title="@string/menu_insert"              
    />     
</menu>

MyActivity.java

@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        super.onCreateOptionsMenu(menu);
        getMenuInflater().inflate(R.menu.list_menu, menu);

        return true;  
    }

我在 Dummies 系列书中读到 ic_menu_add 已经存在于资源中,我不需要添加它,但是当我运行此代码时,它不显示.我尝试添加一个同名的自定义图标,但仍然没有按钮.有人可以帮我吗.

I read in Dummies series book that ic_menu_add is already there in resources and I don't need to add it, but when I run this code it does not display. I've tried to add a custom icon with same name still there is no button. Can someone help me with it please.

推荐答案

如果你使用一个片段,那么你在 onCreate() 中需要这个:

If you use a fragment then you need this in onCreate():

 @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setHasOptionsMenu(true);
    }

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

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