Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/316.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何以编程方式从菜单中删除菜单项?_Java_Android - Fatal编程技术网

Java 如何以编程方式从菜单中删除菜单项?

Java 如何以编程方式从菜单中删除菜单项?,java,android,Java,Android,我正在开发一些Android应用程序,我有一些菜单代码: <menu xmlns:android="http://schemas.android.com/apk/res/android"> <item android:showAsAction="ifRoom" android:id="@+id/menuItemToLeft" android:icon="@drawable/to_left" /> <it

我正在开发一些Android应用程序,我有一些菜单代码:

<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:showAsAction="ifRoom"
        android:id="@+id/menuItemToLeft"
        android:icon="@drawable/to_left" />
    <item
        android:showAsAction="ifRoom"
        android:id="@+id/menuItemToRight"
        android:icon="@drawable/to_right"/>
</menu>

尽量不要使用以下方式显示它们:

@Override
public boolean onPrepareOptionsMenu(Menu menu) {
    super.onPrepareOptionsMenu(menu);
    menu.findItem(R.id.menuItemToLeft).setVisible(true);
    menu.findItem(R.id.menuItemToRight).setVisible(false);
    return true;
}
//true or false depending on your requirements
或删除:

menu.removeItem(x); //where x is the number of the menu item from 0,1,...

然后,您可能需要使用menu.Add()再次创建菜单项。这是一个非常小的解决方案:

public class MainActivity extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener {
Menu myMenu;


protected void onCreate(Bundle savedInstanceState) {
.....
}

   @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        myMenu = menu;
        return true;
    }

 @Override
    public boolean onNavigationItemSelected(MenuItem item) {

int id = item.getItemId();
if (id == R.id.feedsenglish)
        {
            myMenu.findItem(R.id.allfeeds).setVisible(false);
        }
}

OnPrepareOptions菜单只调用一次,而不是每次更改选项卡时都调用。您是对的,但用户应该调用“InvalidateOptions菜单”以执行“OnPrepareOptions菜单”again@Stephen沃克,如果你把马尔科里的观点加入你的答案中,它将变得更容易阅读。
public class MainActivity extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener {
Menu myMenu;


protected void onCreate(Bundle savedInstanceState) {
.....
}

   @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        myMenu = menu;
        return true;
    }

 @Override
    public boolean onNavigationItemSelected(MenuItem item) {

int id = item.getItemId();
if (id == R.id.feedsenglish)
        {
            myMenu.findItem(R.id.allfeeds).setVisible(false);
        }
}