Why doesn't the context menu appear in the center of the screen?

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-24 09:33:04

问题


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

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