简体   繁体   English

动态添加的菜单项在导航抽屉中单击时消失

[英]Dynamically added menu items disappear on click in Navigation Drawer

I followed the Android Developer documentation to create a navigation drawer. 我遵循Android Developer文档来创建导航抽屉。

Then I wanted to add 2 groups of menu items. 然后,我想添加2组菜单项。 One for user created lists, the other for labels. 一个用于用户创建的列表,另一个用于标签。 Since I couldn't directly set titles to groups, I followed the tutorial HERE and wrapped groups in tags. 由于无法直接将标题设置为组,因此我按照此处的教程进行操作,并将组包装在标签中。

My XML menu looks like this: 我的XML菜单如下所示: XML菜单

In onCreate I find the NavigationView using its id, then get its menu, get the first by its id, get the item's SubMenu and add MenuItems: 在onCreate中,我使用其ID查找NavigationView,然后获取其菜单,通过其ID获取第一个菜单,获取该项目的SubMenu并添加MenuItems:

NavigationView navigationView = findViewById(R.id.nav_view);
Menu menu =  navigationView.getMenu();
MenuItem item = menu.findItem(R.id.item_lists);
SubMenu sbLists = item.getSubMenu();
sbLists.add(0, 0, 0, "Android").setIcon(R.drawable.ic_android);
sbLists.add(0, 1, 0, "iOS").setIcon(R.drawable.ic_ios);

And: 和:

navigationView.setNavigationItemSelectedListener(
    menuItem -> {
        // set item as selected to persist highlight
        menuItem.setChecked(true);
        // close drawer when item is tapped
        mDrawerLayout.closeDrawers();

When I click the items added in XML, the drawer closes and if I open it again I can see the item is selected. 当我单击以XML添加的项目时,抽屉关闭,如果再次打开它,则可以看到该项目已被选中。 However, with the items added in onCreate, this is what I see after I re-open the drawer: 但是,在onCreate中添加了项目之后,这是我重新打开抽屉后看到的内容:

Clicking this one 点击这个 点击之前

It's gone 它消失了 后

I've tried searching on Stack Overflow and found only one question with the same problem Navigation item's title disappeared when clicked , however, the solution is to set the item text color to black. 我尝试在Stack Overflow上进行搜索,但发现只有一个问题与该问题相同。单击时导航项的标题消失了 ,但是,解决方案是将项文本颜色设置为黑色。 That doesn't fix the item not being selected problem and doesn't fix the icon disappearing either. 这不能解决未选中项目的问题,也不能解决图标消失的问题。

Please let me know what I'm doing incorrectly, thank you! 请让我知道我做错了什么,谢谢!

Edit: Here's what my Theme looks like in styles.xml: 编辑:这是我的主题在styles.xml中的样子:

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="windowActionModeOverlay">true</item>
    <item name="colorAccent">@color/colorAccent</item>

    <item name="iconColor">@color/icons_light</item>
    <item name="toolbarIconColor">@color/icons_light</item>
    <item name="android:textColorPrimary">@color/primary_text</item>
</style>

I think you need to change the theme of your NavigationView set the colorPrimary and for the text and colorControlHighlight for the selected background element : 我认为您需要更改NavigationView的主题,设置colorPrimary并为所选背景元素设置text和colorControlHighlight

<style name="ThemeOverlay.AppCompat.myTheme">
    <item name="colorPrimary">@color/color-primary</item>
    <item name="colorControlHighlight">@color/color-primary</item>
</style>

and set it in your NavigationView : 并将其设置在NavigationView

<android.support.design.widget.NavigationView
        ...
        app:theme="@style/ThemeOverlay.AppCompat.myTheme">

Solved the problem. 解决了问题。

Even though I had declared android:checkableBehavior="single"> in XML for the group I was adding items to, I still had to manually do setCheckable(true) for each item added programatically. 即使我在XML中为要向其添加项目的组声明了android:checkableBehavior="single"> ,但我仍然必须为以编程方式添加的每个项目手动执行setCheckable(true)

subMenu.add(R.id.group_lists, ITEM_ID, ITEM_ORDER, "item").setIcon(R.drawable.ic_list).setCheckable(true);

Now, when I click an item created in either XML or Java, both the icon and the text are visible, the item is selected. 现在,当我单击以XML或Java创建的项目时,图标和文本均可见,该项目被选中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM