I have my menu which is called by clicking the button on android tablet. But I need to inflate the same menu by a click on view.
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater infl开发者_开发知识库ater = getMenuInflater();
inflater.inflate(R.menu.options, menu);
return true;
}
Any suggestions?
I called
mainActivity.openOptionsMenu();
from View. It worked!. the one Phil suggested creates a different menu. Not an option menu. any way thankx
You will want to use something like:
View myView = findViewById(R.id.my_view);
registerForContextMenu(myView);
in onCreate(...)
. Now when this View
is clicked, it will open the context menu, and you can handle it in onCreateContextMenu(...)
. To be certain that this context menu has the same options as the main menu, you can inflate the same menu with the following code (though it will look like a Context Menu, not like the regular menu):
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
}
精彩评论