[英]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.