我有一个带有菜单项的操作栏。如何隐藏/显示菜单项?

这就是我想做的:

MenuItem item = (MenuItem) findViewById(R.id.addAction);
item.setVisible(false);
this.invalidateOptionsMenu();

当前回答

根据Android开发者官方网站,OnCreateOptionMenu(菜单菜单)不建议在运行时更改菜单项或图标,可见性等。

After the system calls onCreateOptionsMenu(), it retains an instance of the Menu you populate and will not call onCreateOptionsMenu() again unless the menu is invalidated for some reason. However, you should use onCreateOptionsMenu() only to create the initial menu state and not to make changes during the activity lifecycle. If you want to modify the options menu based on events that occur during the activity lifecycle, you can do so in the onPrepareOptionsMenu() method. This method passes you the Menu object as it currently exists so you can modify it, such as add, remove, or disable items. (Fragments also provide an onPrepareOptionsMenu() callback.) --AndroidDeveloper Official Site --

您可以使用这个onOptionsItemSelected(MenuItem项)方法跟踪用户输入。

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    int id = item.getItemId();

    if (id == R.id.edit) {
        Intent intent = new Intent(this, ExampleActivity.class);
        intent.putExtra(BUNDLE_KEY, mConnection);
        startActivityForResult(intent, PICK_CHANGE_REQUEST);
        return true;
    } else if (id == R.id.delete) {
        showDialog(this);
        return true;
    }

    return super.onOptionsItemSelected(item);
}

如果您需要在运行时更改菜单项,您可以使用onprepareoptionmenu(菜单菜单)来更改它们

@Override
public boolean onPrepareOptionsMenu(Menu menu){

    if (Utils.checkNetworkStatus(ExampleActivity.this)) {
        menu.findItem(R.id.edit).setVisible(true);
        menu.findItem(R.id.delete).setVisible(true);
    }else {
        menu.findItem(R.id.edit).setVisible(false);
        menu.findItem(R.id.delete).setVisible(false);
    }
    return true;
} 

其他回答

最初在菜单布局文件中将菜单项可见性设置为false,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <item
        android:visible="false"
        android:id="@+id/action_do_something"
        android:title="@string/txt_do_something"
        app:showAsAction="always|withText"
        android:icon="@drawable/ic_done"/>
</menu>

然后,您可以简单地将菜单项的可见性设置为false,在您的oncreateoptionsmmenu()后,膨胀菜单。

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    super.onCreateOptionsMenu(menu, inflater);
    inflater.inflate(menu,R.menu.menu);
    MenuItem item = menu.findItem(R.id.menuItemId);
    if (item != null){
        item.setVisible(false);
    }
}

获取一个指向这样的菜单项,调用setVisible来调整它的可见性,然后在你的活动上调用invalidateOptionsMenu(),这样动作栏菜单就会相应调整。

更新:菜单项不是常规视图,它是布局的一部分。它很特别,完全不同。你的代码为item返回null,这导致了崩溃。你需要做的是:

MenuItem item = menu.findItem(R.id.addAction);

下面是你应该调用的顺序: 首先调用invalidateOptionsMenu(),然后在onCreateOptionsMenu(Menu)内部获得对MenuItem的引用(通过调用Menu . finditem())并在其上调用setVisible()

Android kotlin,隐藏或设置操作栏中菜单项的可见性。

override fun onCreateOptionsMenu(menu: Menu): Boolean {
    val inflater = menuInflater
    inflater.inflate(R.menu.main_menu, menu)
    val method = menu.findItem(R.id.menu_method)
    method.isVisible = false //if want to show set true
    return super.onCreateOptionsMenu(menu)
}

这个代码对我有用

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main_menu,menu);
    if (Application.sharedPreferences.getInt("type",1) == 2)
    {
        menuItem = menu.findItem(R.id.menu_travel_orders);
        menuItem.setVisible(false);
    }
    return super.onCreateOptionsMenu(menu);
}

这在《Activity》和《Fragment》中都很奏效

@Override
public void onPrepareOptionsMenu(Menu menu) {
    super.onPrepareOptionsMenu(menu);
    if (menu.findItem(R.id.action_messages) != null)
        menu.findItem(R.id.action_messages).setVisible(false);
}