为什么上下文菜单不出现在屏幕中央?

时间:2017-10-02 13:10:50

标签: android contextmenu

我读到上下文菜单应该自动居中显示,但我的上下文菜单会根据我点击的位置更改位置。当我点击第一个列表项(右下角的菜单)时会发生This,而其他项目会发生this

这是我的代码:

@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>

0 个答案:

没有答案