机器人在运行时添加菜单项 [英] Android add a menu item at runtime

查看:120
本文介绍了机器人在运行时添加菜单项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.main_menu, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle item selection
    switch (item.getItemId()) {
            case R.id.about:
            Intent i = new Intent(this, impresszum.class);
            startActivity(i);

                return true;
            case R.id.quit:
                AppUtils.ExitTheApplication();
                return true;
            default:
                return super.onOptionsItemSelected(item);
    }
}

我有这样的code。我想在运行时添加一些菜单,当我需要它。并删除了一些菜单,当我需要它。我该怎么做?

I have this code. I want to add some menu at runtime, when i need it. And remove some menu when i need it. How can I do that?

推荐答案

菜单项不一定)与像由雪莉提出的部分活动,在这种情况下,你可以叫 invalidateOptionsMenu(来获取菜单对象上举行,以刷新在随后的调用菜单上prepareOptionsMenu()

Menu items are not necessarily related to an activity like in the section suggested by Shelly, in which case you can call invalidateOptionsMenu() to get a hold on the menu object in order to "refresh" the menu in the subsequent call to onPrepareOptionsMenu().

同Android开发者网站:

在Android 2.3.x版本和更低的系统调用<一个href=\"http://developer.android.com/reference/android/app/Activity.html#on$p$ppareOptionsMenu%28android.view.Menu%29\"相对=nofollow>在prepareOptionsMenu()
  每次用户打开选项菜单(presses菜​​单按钮)。

On Android 2.3.x and lower, the system calls onPrepareOptionsMenu() each time the user opens the options menu (presses the Menu button).

在Android 3.0及更高版本,选项菜单被认为永远
  当菜单项操作栏中的psented $ P $打开。当事件
  发生并且要执行菜单更新,您必须调用
   invalidateOptionsMenu()来请求系统调用
  <一href=\"http://developer.android.com/reference/android/app/Activity.html#on$p$ppareOptionsMenu%28android.view.Menu%29\"相对=nofollow>在prepareOptionsMenu()。

On Android 3.0 and higher, the options menu is considered to always be open when menu items are presented in the action bar. When an event occurs and you want to perform a menu update, you must call invalidateOptionsMenu() to request that the system call onPrepareOptionsMenu().

这篇关于机器人在运行时添加菜单项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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