How to set the title of a menu in Android

倾然丶 夕夏残阳落幕 提交于 2020-05-29 08:25:16

问题


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.

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.


回答1:


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

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 onPrepareOptionsMenu to update the menu items each time the menu is displayed.




回答2:


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");



回答3:


To dynamically set menu title, you'd do this in 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.




回答4:


Just use :

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

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




回答5:


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

 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)
}


来源:https://stackoverflow.com/questions/19617132/how-to-set-the-title-of-a-menu-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!