如何在Android中设置菜单的标题 [英] How to set the title of a menu in Android
问题描述
我正在尝试动态设置菜单的标题.
I'm trying to dynamically set the title of my menu.
检索和设置它:
ItemView menuTitle = ((ItemView) findViewById(R.id.menu_filter));
menuTitle.setTitle("TITLE_HERE");
只要它在 onOptionsItemSelected(MenuItem item)
方法中就可以正常工作.
works just fine so long as it's in the onOptionsItemSelected(MenuItem item)
method.
我无法从 onPrepareOptionsMenu
或 onCreateOptionsMenu
方法中找到一种方法来设置它,因为 findViewById
返回 null(即使在膨胀菜单).
I haven't been able to find a way to set this from the onPrepareOptionsMenu
or onCreateOptionsMenu
methods as findViewById
returns null (even after inflating the menu).
奇怪的是,文档中似乎没有任何内容可以实现这一点,而且对于这样一个看似简单的问题,谷歌搜索并没有发现太多.
Oddly enough there doesn't seem to be anything in the documentation for accomplishing this and google searches haven't turned up much for such a seemingly simple problem.
推荐答案
我一定是刚刚发现并没有意识到 Menu 有一个名为 findItem 的函数...
I must have just derped and not realized that there is a function for Menu called findItem...
menu.findItem(R.id.MENU_TITLE).setTitle("MY TITLE");
这篇关于如何在Android中设置菜单的标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!