Java 如何在Android中设置菜单的标题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19617132/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 18:49:36  来源:igfitidea点击:

How to set the title of a menu in Android

javaandroidmenutitle

提问by user2884265

I'm trying to dynamically set the title of my menu.

我正在尝试动态设置菜单的标题。

Retrieving and setting it as such:

检索和设置它:

ItemView menuTitle = ((ItemView) findViewById(R.id.menu_filter));
menuTitle.setTitle("TITLE_HERE");

works just fine so long as it's in the onOptionsItemSelected(MenuItem item)method.

只要它在onOptionsItemSelected(MenuItem item)方法中就可以正常工作。

I haven't been able to find a way to set this from the onPrepareOptionsMenuor onCreateOptionsMenumethods as findViewByIdreturns null (even after inflating the menu).

我一直无法找到一种方法将其从onPrepareOptionsMenuoronCreateOptionsMenu方法设置为findViewById返回 null(即使在菜单膨胀之后)。

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.

奇怪的是,文档中似乎没有任何内容可以完成此操作,而对于这样一个看似简单的问题,谷歌搜索并没有发现太多内容。

采纳答案by Jitesh Dalsaniya

I would suggest keeping a reference within the activity to the Menu object you receive in onCreateOptionsMenuand then using that to retrieve the MenuItemthat requires the change as and when you need it. For example, you could do something along the lines of the following:

我建议在活动中保留对您收到的 Menu 对象的引用onCreateOptionsMenu,然后使用它来检索MenuItem需要更改的对象。例如,您可以按照以下方式进行操作:

public class YourActivity extends Activity {

    private Menu menu;
    private String inMenuTitle = "Set to In";
    private String outMenuTitle = "Set to Out";
    private boolean flag = false;

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);

        // Create your menu...

        this.menu = menu;
        return true;
    }

    private void updateMenuTitles() {
        MenuItem menuItem = menu.findItem(R.id.bedSwitch);
        if (flag) {
            menuItem .setTitle(outMenuTitle);
        } else {
            menuItem .setTitle(inMenuTitle);
        }
    }

}

Alternatively, you can override onPrepareOptionsMenuto update the menu items each time the menu is displayed.

或者,您可以覆盖onPrepareOptionsMenu以在每次显示菜单时更新菜单项。

回答by CSmith

To dynamically set menu title, you'd do this in onCreateOptionsMenu:

要动态设置菜单标题,您可以在 onCreateOptionsMenu 中执行此操作:

public boolean onCreateOptionsMenu(Menu menu) 
{
      menu.add(Menu.NONE, MyActivity.MENU_ID, 0, "Your Title").setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_WITH_TEXT);
    return (true);
}

and as conditions change (i.e. as your title needs to change), call

并且随着条件的变化(即您的标题需要更改),请致电

invalidateOptionsMenu();

on your activity, which will call onCreateOptionsMenu and set the new title.

在您的活动上,这将调用 onCreateOptionsMenu 并设置新标题。

回答by user2884265

I must have just derped and not realized that there is a function for Menu called findItem...

我一定是刚刚发现并没有意识到菜单有一个名为 findItem 的函数......

menu.findItem(R.id.MENU_TITLE).setTitle("MY TITLE");

回答by Maifee Ul Asad

Just use :

只需使用:

        getSupportActionBar().setTitle("YOUR TITLE HERE");

Inside on create, after initializing ui or viewbinding or databinding.

create内部,在初始化 ui 或 viewbinding 或 databinding 之后。

回答by Nabin Khatiwada

I am changing the title of the menu from onPrepareOptionMenusomething like the below code. This is the code from my fragment, I think this should also work on the activity.

我正在从onPrepareOptionMenu类似下面的代码中更改菜单的标题。这是我的片段中的代码,我认为这也应该适用于活动。

 override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
    inflater.inflate(R.menu.menu_profile, menu)
    super.onCreateOptionsMenu(menu, inflater)
}

override fun onPrepareOptionsMenu(menu: Menu) {
    super.onPrepareOptionsMenu(menu)
    if (count == 1) {
        menu.findItem(R.id.screen_count).title = "1/3"
    } else {
        menu.findItem(R.id.screen_count).title = "xyz/3"
    }
}

override fun onOptionsItemSelected(item: MenuItem): Boolean {
    when (item.itemId) {
        R.id.ic_edit -> {
            // handle click event here
        }
    }
    return super.onOptionsItemSelected(item)
}