菜单目录丢失 [英] menu directory is missing

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

问题描述

我是一名非常新的 Android 程序员.我正在尝试在我的应用程序中定义一个操作溢出按钮.从我读过的内容来看,它涉及修改 menu.xml 文件.我在我的应用程序中找不到该文件,而且我没有 res >菜单目录.我创建了一个 SettingActivity.有什么建议吗?

I'm a very new programmer in Android. I'm trying to define an action overflow button in my app. From what I've read it involves modifying the menu.xml file. I cannot find that file in my app and I don't have the res > menu directory. I have created a SettingActivity. Any Suggestions?

推荐答案

您可以在 res/文件夹中创建菜单目录.在Android Studio 的项目视图中右键单击res,然后单击新建->Android 资源目录".然后选择资源类型"下的菜单.然后,您可以将一个文件添加到包含您的菜单项的新 res/menu 目录中,例如 (res/menu/main_menu.xml)

You can create a menu dir in the res/ folder. Right click on res in the project view in Android Studio and click new -> "Android Resource Directory". Then select menu under "Resource Type". You can then add a file to that new res/menu directory that contains your menu items like this (res/menu/main_menu.xml)

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <item
        android:id="@+id/action"
        android:title="@string/action"
        app:showAsAction="always" />
</menu>

并确保像这样覆盖 MainActivity 类中的 onCreateOptionsMenu(Menu menu):

And get sure to override the onCreateOptionsMenu(Menu menu) in the MainActivity class like this:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.main_menu,menu);
    return true;
}

这篇关于菜单目录丢失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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