MENU中不允许使用元素项目

Pet*_* O. 6 android android-layout android-2.2-froyo android-menu

我正在尝试制作自定义选项菜单.使用此代码后,我得到:此处不允许使用元素项

码:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_height="fill_parent"
        android:layout_width="fill_parent">
    <item android:id="@+id/morsoid_settings"
          android:icon="@drawable/ic_new_game"
          android:title="@string/new_game" />
    <item android:id="@+id/morsoid_close"
          android:icon="@drawable/ic_help"
          android:title="@string/help" />
</menu>
Run Code Online (Sandbox Code Playgroud)

灵感来自:Android开发指南

Fre*_*mpe 12

我不知道它是否有所作为,但你是否将菜单放在res/menu中而不是res/layout中?

  • 这个答案对我有用.当然在IntelliJ Idea 11.0.2中它一直在我的菜单布局中显示错误.一旦我在res/menu下移动它就可以了.+1 (2认同)

Mat*_*lis 3

尝试忽略布局属性。这是文档中的示例:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+id/new_game"
          android:icon="@drawable/ic_new_game"
          android:title="@string/new_game" />
    <item android:id="@+id/help"
          android:icon="@drawable/ic_help"
          android:title="@string/help" />
</menu>
Run Code Online (Sandbox Code Playgroud)

编辑- 还要确保您MenuInflater按照指南建议使用 a :

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.game_menu, menu);
    return true;
}
Run Code Online (Sandbox Code Playgroud)

使用 aLayoutInflater将导致<menu>被解释为视图元素,而实际上它是菜单资源。

  • 我已经尝试过,但显示了另一个错误: **layout_width should be Defined** (高度相同):( (2认同)