Trying to hide/disable entire menu (overflow) in fragment

你离开我真会死。 提交于 2019-12-05 02:16:48

问题


I have been unsuccessful in trying to hide or disable the overflow menu in one fragment.

I have tried setting setHasOptionsMenu(false) with no success, and then I tried setHasOptionsMenu(true) and tried inflating with an empty menu like below.

Both attempts do not work for me.

How do I hide or disable the options/overflow menu in one fragment only??

Thanks in advance!

Fragment

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setHasOptionsMenu(true);
}

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    // Inflate the menu; this adds items to the action bar if it is present.
    inflater.inflate(R.menu.empty, menu);
    super.onCreateOptionsMenu(menu, inflater);
}

回答1:


Figured out how to do it. Just need to set the menu items I don't want visible. In my case, I set all of them not visible and that removed the overflow menu entirely.

Also don't forget you have to setHasOptionsMenu(true) in your onCreate so it knows to call onCreateOptionsMenu

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    if (menu != null) {
        menu.findItem(R.id.menu_settings).setVisible(false);
        menu.findItem(R.id.menu_leave_feedback).setVisible(false);
        menu.findItem(R.id.menu_shop).setVisible(false);
    }
}



回答2:


I know this answer is very late but I like to share it.
In Fragment onCreate method:

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setHasOptionsMenu(true);

    }


And in Fragment onCreateOptionsMenu() method:

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        super.onCreateOptionsMenu(menu, inflater);

        menu.clear();
    }



回答3:


 setHasOptionsMenu(true)

in OnCreate() and

 @Override
        public void onPrepareOptionsMenu(Menu menu) {
            // if nav drawer is opened, hide the action items       
                menu.findItem(R.id.xxx).setVisible(false);
                menu.findItem(R.id.yyy).setVisible(false);              
        }


来源:https://stackoverflow.com/questions/24538422/trying-to-hide-disable-entire-menu-overflow-in-fragment

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