如何在Android中设置菜单的标题 [英] How to set the title of a menu in Android

查看:63
本文介绍了如何在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.

我无法从 onPrepareOptionsMenuonCreateOptionsMenu 方法中找到一种方法来设置它,因为 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屋!

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