问题
I read that the context menu should appear centered automatically, but my context menu changes positions based on where I click. This is what happens when I click on the first list item (menu in the lower right corner), and this is what happens for the other items.
Here is my code:
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo;
menu.setHeaderTitle("E adesso?");
MenuInflater inflater = getActivity().getMenuInflater();
inflater.inflate(R.menu.context_menu, menu);
mvc.controller.setHDImage(getContext(), info.position);
}
@RequiresApi(api = Build.VERSION_CODES.N)
@Override
public boolean onContextItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.same_author_images:
return true;
case R.id.share:
shareImage(item);
return true;
default:
return super.onContextItemSelected(item);
}
}
context_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/same_author_images"
android:title="Immagini stesso autore" />
<item
android:id="@+id/share"
android:actionProviderClass="android.widget.ShareActionProvider"
android:title="Condividi" />
</menu>
来源:https://stackoverflow.com/questions/46526166/why-doesnt-the-context-menu-appear-in-the-center-of-the-screen