我无法将子菜单项设置为选中状态 [英] I am unable to set a submenu item as checked

查看:15
本文介绍了我无法将子菜单项设置为选中状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的抽屉使用了 NavigationView.这是 menus.xml 文件,它定义了抽屉的所有菜单项:

I am using a NavigationView for my drawer. This is the menus.xml file which defines all the menu items for the drawer:

<?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/item_1"
            android:title="Item 1"
            android:checked="true" />
        <item
            android:id="@+id/item_2"
            android:title="Item 2" />
        <item
            android:id="@+id/item_3"
            android:title="Item 3" />
        <item
            android:id="@+id/item_4"
            android:title="Item 4" />
        <item android:title="List 1">
            <menu>
                <item
                    android:id="@+id/subitem_1"
                    android:title="SubItem 1" />
                <item
                    android:id="@+id/subitem_2"
                    android:title="SubItem 2" />
            </menu>
        </item>
        <item android:title="List 2">
            <menu>
                <item
                    android:id="@+id/subitem_3"
                    android:title="SubItem 3" />
                <item
                    android:id="@+id/subitem_4"
                    android:title="SubItem 4" />
            </menu>
        </item>
    </group>
</menu>

事情是当我在子菜单项上调用 setChecked(true) 时,它不会突出显示.

Thing is when I call setChecked(true) on the sub menu items it doesn't get highlighted.

因此,如果我在子菜单项上强制使用属性 android:checkable="true",则需要单击两次才能突出显示.此外,如您所见,项目 1 已突出显示,并且 checkableBehaviour 设置为 single.在这种情况下,子菜单和第一项保持突出显示.为什么在运行 setChecked(true) 时子菜单没有突出显示?

So if I force the attribute android:checkable="true" on the sub-menu items, it takes two clicks to have it highlighted. Also, as you can see Item 1 is highlighted and the checkableBehaviour is set to single. In this case, the sub menu and the first item remain highlighted. Why is the sub menu not getting highlighted on running setChecked(true)?

推荐答案

对于子菜单应该添加带有 android:checkableBehavior="single"

For submenu should be added the group with android:checkableBehavior="single"

你的 xml 应该是这样的:

Your xml should be something like this:

<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group android:checkableBehavior="single">
    <item
        android:id="@+id/item_1"
        android:checked="true"
        android:title="Item 1" />
    <item
        android:id="@+id/item_2"
        android:title="Item 2" />
    <item
        android:id="@+id/item_3"
        android:title="Item 3" />
    <item
        android:id="@+id/item_4"
        android:title="Item 4" />
    <item android:title="List 1">
        <menu>
            <group android:checkableBehavior="single">
                <item
                    android:id="@+id/subitem_1"
                    android:title="SubItem 1" />
                <item
                    android:id="@+id/subitem_2"
                    android:title="SubItem 2" />
            </group>
        </menu>
    </item>
    <item android:title="List 2">
        <menu>
            <group android:checkableBehavior="single">
                <item
                    android:id="@+id/subitem_3"
                    android:title="SubItem 3" />
                <item
                    android:id="@+id/subitem_4"
                    android:title="SubItem 4" />
            </group>
        </menu>
    </item>
</group>

这篇关于我无法将子菜单项设置为选中状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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