简体   繁体   English

如何在 NavigationView 中以编程方式更改 MenuItem 的标题?

[英]How can I change MenuItem's title programatically inside NavigationView?

I successfully implemented in my app a "side menu" with NavigationView and creating a custom ActionBar, following the instructions within this tutorial .我按照本教程中的说明成功地在我的应用程序中实现了带有 NavigationView 的“侧面菜单”并创建了自定义 ActionBar。 It looks like this:它看起来像这样:

在此处输入图像描述

That's great, it works... it opens by "hamburger" button in ActionBar and by sliding finger left to right;太好了,它有效……它通过 ActionBar 中的“汉堡包”按钮和从左向右滑动手指打开; I can attach functionality to those options.我可以将功能附加到这些选项。 But, I'd like to change options title dinamically , by Java. About that, I found some other StackOverflow question and tried implementing the solution in the answer.但是,我想通过 Java 动态更改选项标题。关于这个,我发现了一些其他 StackOverflow 问题并尝试在答案中实施解决方案。 The result was... unexpected, but curious:结果是......出乎意料,但很好奇:

I override onPrepareOptionsMenu in MainActivity :我在MainActivity中覆盖onPrepareOptionsMenu

@Override
public boolean onPrepareOptionsMenu(Menu menu) {
    menu.clear();
    getMenuInflater().inflate(R.menu.main_menu, menu);
    MenuItem option3 = menu.findItem(R.id.nav_option3);
    option3.setTitle("Option3 -> " + option3_cnt + " elements");
    MenuItem option4 = menu.findItem(R.id.nav_option4);
    option4.setTitle("Option4 -> " + option4_cnt + " elements");

    return super.onPrepareOptionsMenu(menu);
}

What this actually did, was creating a copy of the NavigationView menu, but in a "three-dot button" at the right side of ActionBar (that button wasn't there before implementing onPrepareOptionsMenu , BTW).这实际上做了什么,是创建NavigationView菜单的副本,但是在 ActionBar 右侧的“三点按钮”中(顺便说一句,在实现onPrepareOptionsMenu之前该按钮不存在)。 And yes, it shows the counters;是的,它显示了计数器; but the idea was doing exactly that thing, but with the "side menu" of the NavigationView, instead of that old-fashioned "options menu".但是这个想法正是在做那件事,但是使用 NavigationView 的“侧面菜单”,而不是那个老式的“选项菜单”。 How can I achieve that?我怎样才能做到这一点?

By the way, MenuItem s have a XML structure like this within main_menu.xml :顺便说一句, MenuItem在 main_menu.xml 中有一个main_menu.xml结构:

<item
    android:id="@+id/nav_option3"
    android:icon="@drawable/ic_launcher_foreground"
    android:title="Option3" />
<item
    android:id="@+id/nav_option4"
    android:icon="@drawable/ic_launcher_foreground"
    android:title="Option4" />

I'd like, perhaps, to add more elements to the MenuItem s themselves;也许,我想向MenuItem本身添加更多元素; so I could place the counter in some kind of "custom layout" attached to the MenuItem or something like that... but for now, I conform with attaching the count to the title itself.所以我可以将计数器放在某种附加到MenuItem或类似东西的“自定义布局”中......但是现在,我同意将计数附加到标题本身。

You can just get the menu from NavigationView & do the findItem to get access to MenuItem which then you can use to setTitle .您只需从NavigationView获取菜单并执行findItem即可访问MenuItem ,然后您可以使用它来设置setTitle

Example:例子:

    // your navigationView
    NavigationView navigationView = findViewById(R.id.navigation_view);
    
    // item you want to change, choose your id here
    MenuItem menuItem = navigationView.getMenu().findItem(R.id.nav_gallery);
    menuItem.setTitle("Changed at runtime");

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

相关问题 如何在NavigationView的xml文件中更改菜单包含项的标题颜色 - How to change title color of menu's enclosing item in NavigationView's xml files 更改menuItem的标题 - change title of menuItem 如何更改JFrame标题的字体? - How can I change the font of the JFrame`s title? 如何以编程方式更改LinearLayout的重力? - How can I programatically change the gravity for a LinearLayout? 如何在NavigationView中动态添加自定义MenuItem? - How to add custom MenuItem dynamically in NavigationView? Java:如何更改JScrollPane内部的JPanel的大小? - Java: how can I change the size of a JPanel that's inside a JScrollPane? 带有 actionLayout 的 NavigationView 菜单项。 如何为每个项目设置这些动作布局的属性? - NavigationView menu items with actionLayout. How can I set those action layout's attribute for each item? 为什么PreferenceCategory的标题和复选框的颜色这么浅,我该如何更改? - Why PreferenceCategory's title & checkBox color is so light & how can I change it? 如何以编程方式更改列表视图内的卡片视图颜色? - How to change a cardview color inside a listview programatically? 如何以编程方式更改选择器内部形状的颜色? - How to change the color of a shape that is inside a selector programatically?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM