操作栏菜单项在运行时重复

时间:2016-08-17 14:33:18

标签: android android-studio android-actionbar android-actionbar-compat

我是Java编程和Android Studio的新手。 在创建选项菜单时,我在主菜单中创建了一个选项列表,其中包含组子菜单中的两个项目。

这是我的menu.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    >
    <item
        android:id="@+id/giveup_setting"
        android:orderInCategory="1"
        app:showAsAction="ifRoom"
        android:title="@string/action_option1"/>
    <item
        android:id="@+id/new_game_setting"
        android:orderInCategory="2"
        app:showAsAction="never"
        android:title="@string/action_option2"/>
    <item
        android:id="@+id/help_setting"
        android:orderInCategory="3"
        app:showAsAction="never"
        android:title="@string/action_option3"/>

    <item
        android:id="@+id/settings_setting"
        android:orderInCategory="4"
        app:showAsAction="never"
        android:title="@string/action_option4" />
    <menu>
    <group android:checkableBehavior="single">
        <item
            android:id="@+id/menu_color"
            android:orderInCategory="1"
            app:showAsAction="never"
            android:title="@string/layout_color"/>
        <item
            android:id="@+id/menu_text"
            android:orderInCategory="2"
            app:showAsAction="never"
            android:title="@string/layout_text"/>
    </group>
    </menu>
    <item
        android:id="@+id/about_setting"
        android:orderInCategory="5"
        app:showAsAction="never"
        android:title="@string/action_option5"/>
    <item
        android:id="@+id/exit_setting"
        android:orderInCategory="6"
        app:showAsAction="never"
        android:title="@string/action_option6"/>

</menu>

这就是我在MainActivity中夸大菜单的方式:

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

    @Override
    public boolean onOptionsItemSelected(MenuItem menu) {
        // Handle item selection
        switch (menu.getItemId()) {
            case R.id.help_setting:
                //show help screens

            return true;

            case R.id.menu_color:
                //change a layout of my main activity
            return true;

            case R.id.menu_text:
                   //change another layout of my main activity
            return true;*/

            case R.id.about_setting:
                   //show my about screen
            return true;

            case R.id.giveup_setting:
                //resets the game

            case R.id.new_game_setting:
                //restarts the app
                return true;

            case R.id.exit_setting:
                //exits the app
                return true;
            default:
                return super.onOptionsItemSelected(menu);
        }

}

在运行应用程序时,菜单会填充,但“设置”项目(具有嵌套组单选按钮)会显示两次。

On running the app, the menu populates but the "Settings" item (which has the nested group radio buttons) displays twice.

我做错了什么?

1 个答案:

答案 0 :(得分:0)

如果android:id="@+id/settings_setting"是您要拥有子菜单的那个,请不要关闭item tag。而是在<menu>代码中夸大<item>

<item
  android:id="@+id/settings_setting"
  android:orderInCategory="4"
  app:showAsAction="never"
  android:title="@string/action_option4">
  <menu>
    <group android:checkableBehavior="single">
        <item
            android:id="@+id/menu_color"
            android:orderInCategory="1"
            app:showAsAction="never"
            android:title="@string/layout_color"/>
        <item
            android:id="@+id/menu_text"
            android:orderInCategory="2"
            app:showAsAction="never"
            android:title="@string/layout_text"/>
    </group>
  </menu>
</item>