开发者

How to inflate options menu from ponTouchEvent(MotionEvent me) in View

开发者 https://www.devze.com 2023-03-29 03:53 出处:网络
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.

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);
}
0

精彩评论

暂无评论...
验证码 换一张
取 消