Android - 在SubMenu中查找项目的ID

时间:2016-06-19 07:26:25

标签: java android

我使用导航活动从一个Activity移动到另一个Activity。我想在NavigationView菜单中添加子菜单<但无法找到在子菜单中找到id og项目的方法。

    <menu>    
<item android:title="Communicate">
    <menu>
        <item android:id="@+id/about_us" android:icon="@android:drawable/ic_menu_share"
            android:title="About us" />
    </menu>
</item></menu>

@Override
public boolean onNavigationItemSelected(MenuItem item) {
    // Handle navigation view item clicks here.
    int id = item.getItemId();

    switch (id) {
        case R.id.nav_order: {
            break;
        }
        case R.id.nav_search: {
            break;
        }
    }

    return true;
}

我想添加:case R.id.about_us。 但Android Studio无法找到此ID。 我怎么能做到这一点,我做错了什么?

2 个答案:

答案 0 :(得分:2)

您的代码对我来说很好。菜单中缺少一件事:

<menu xmlns:android="http://schemas.android.com/apk/res/android"/> <!--the xmlns is missing -->
</menu>

答案 1 :(得分:0)

尝试这样写:

 The drawer itesm below have been classified into groups

    <group
        android:id="@+id/group_1"
        android:checkableBehavior="single">
        <item
            android:id="@+id/navigation_item_1"
            android:icon="@drawable/ic_number_0"
            android:title="@string/navigation_item_1" />
        <item
            android:id="@+id/navigation_item_2"
            android:icon="@drawable/ic_number_1"
            android:title="@string/navigation_item_2" />
    </group>
    <group
        android:id="@+id/group_2"
        android:checkableBehavior="single">
        <item
            android:id="@+id/navigation_item_3"
            android:icon="@drawable/ic_number_2"
            android:title="@string/navigation_item_3" />
        <item
            android:id="@+id/navigation_item_4"
            android:icon="@drawable/ic_number_3"
            android:title="@string/navigation_item_4" />
    </group>
    <group
        android:id="@+id/group_3"
        android:checkableBehavior="single">
        <item
            android:id="@+id/navigation_item_5"
            android:icon="@drawable/ic_number_4"
            android:title="@string/navigation_item_5" />
    </group>

    OR
    The drawer items below have been categorized according to headers
    <item
        android:id="@+id/navigation_item_1"
        android:icon="@drawable/ic_number_0"
        android:title="@string/navigation_item_1">
        <menu>
            <item
                android:id="@+id/navigation_item_2"
                android:icon="@drawable/ic_number_1"
                android:title="@string/navigation_item_2" />
            <item
                android:id="@+id/navigation_item_3"
                android:icon="@drawable/ic_number_2"
                android:title="@string/navigation_item_3" />
        </menu>
    </item>

    <item
        android:id="@+id/navigation_item_4"
        android:icon="@drawable/ic_number_3"
        android:title="@string/navigation_item_4">
        <menu>
            <item
                android:id="@+id/navigation_item_5"
                android:icon="@drawable/ic_number_4"
                android:title="@string/navigation_item_5" />
        </menu>

</item>