0

我有这个 XML。

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <group android:checkableBehavior="single">
        <item
            android:id="@+id/nav_home"
            android:checked="true"
            android:icon="@drawable/ic_home_black_24dp"
            android:title="Home" />
        <item
            android:id="@+id/nav_search"
            android:icon="@drawable/ic_search_black"
            android:title="Search Location" />
        <item
            android:id="@+id/nav_fav"
            android:icon="@drawable/ic_favorite"
            android:title="Favorites" />
        <item
            android:id="@+id/nav_recent"
            android:icon="@drawable/ic_nav_route"
            android:title="Recent Location" />
        <item
            android:id="@+id/nav_route"
            android:icon="@drawable/ic_place"
            android:title="Route" />
    </group>

    <item android:title="Others">
        <menu>
            <item
                android:id="@+id/nav_settings"
                android:icon="@drawable/ic_settings"
                android:title="Settings" />
            <item
                android:id="@+id/nav_about"
                android:icon="@android:drawable/ic_menu_send"
                android:title="About" />
        </menu>
    </item>

</menu>

但是在选择 nav_settings 时,
该项目未选中。
该项目应该像上面的其他项目一样检查。
这是我设置检查项目的代码。
该片段将正常工作。
但是 nav_settings 上的选择器不起作用..

 public void selectDrawerItem(MenuItem menuItem) {
            DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
            drawer.closeDrawer(GravityCompat.START);
            Fragment fragment = null;

            Class fragmentClass;
            switch(menuItem.getItemId()) {
                case R.id.nav_home:
                    fragmentClass = FragmentMap.class;
                    setTitle("Map");
                    break;

                case R.id.nav_search:
                    fragmentClass = FragmentSearchLoc.class;
                    Global.setCurrentItem =2;
                    break;
                case R.id.nav_recent:
                    fragmentClass = FragmentSearchLoc.class;
                    Global.setCurrentItem =0;
                    break;
                case R.id.nav_fav:
                    fragmentClass = FragmentSearchLoc.class;
                    Global.setCurrentItem =1;
                    break;
                case R.id.nav_route:
                    fragmentClass = FragmentRoute.class;
                    setTitle("Routing");
                    break;
                case R.id.nav_settings:
                    fragmentClass = FragmentSettings.class;
                    setTitle("Settings");
                    break;
                default:
                    fragmentClass = FragmentMap.class;
            }

            try {
                fragment = (Fragment) fragmentClass.newInstance();
            } catch (Exception e) {
                e.printStackTrace();
            }


            FragmentManager fragmentManager = getSupportFragmentManager();
            fragmentManager.beginTransaction().replace(R.id.flContent, fragment).commit();

            drawer
            menuItem.setChecked(true);
            drawer.closeDrawers();
        }
4

1 回答 1

0

我认为不允许嵌套像您的代码这样的项目。

如果您喜欢以下内容,它可能会起作用:

<group android:checkableBehavior="single">
     <item
         android:id="@+id/nav_settings"
         android:icon="@drawable/ic_settings"
         android:title="Settings" />
     <item
         android:id="@+id/nav_about"
         android:icon="@android:drawable/ic_menu_send"
         android:title="About" />
</group>
于 2015-11-28T03:57:26.020 回答