Android ListView中的弹出菜单问题

Har*_*had 8 android listview popupmenu android-menu

我是Android新手,我正在创建Listview弹出菜单.但我有它widthheight问题.弹出菜单可以采用更高的高度和宽度.SO中有很多问题,但这些都没有帮助我.

创建弹出菜单,我尝试了以下方法.

1] 使用弹出菜单和下面的代码:

 private void showPopupMenu(View view){
        Context wrapper = new ContextThemeWrapper(this, R.style.PopupMenu);
        PopupMenu popupMenu = new PopupMenu(wrapper,view);

        popupMenu.getMenuInflater().inflate(R.menu.popup_menu,popupMenu.getMenu());
        popupMenu.show();

        popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener(){
            @Override
        public boolean onMenuItemClick(MenuItem item){
                switch (item.getItemId()){
                    case R.id.install:
                        Intent intent = new Intent(ViewAllRelationActivity.this,EditRelativeActivity.class);
                        startActivity(intent);
                        break;
                    case R.id.addtowishlist:
                        break;

                }
                return false;
            }
        });
    }
Run Code Online (Sandbox Code Playgroud)

它给出了这个输出:

在此输入图像描述

2] 使用ContextMenu,它显示以下输出:

我们可以在ContextMenu中保持宽度和高度但它始终显示在Center中 not each row of our Listview Data.

在此输入图像描述

但我想要below Image type Popup menu.宽度和高度都很小.

在此输入图像描述

请为此提供解决方案.

Bha*_*nik 2

您可以通过编程方式添加这些选项,而无需使用 xml 文件,如下所示,也许这可以帮助您。

这里“一”和“二”显示了您在弹出菜单中给出的选项的索引。就像在第一个位置编辑帖子在第二个位置删除帖子等。

1)在图像上单击打开弹出菜单:

private final static int ONE = 1;
private final static int TWO = 2;

PopupMenu popupMenu = new PopupMenu(this, findViewById(R.id.img_detail_information_options));
popupMenu.getMenu().add(Menu.NONE, ONE, Menu.NONE, getResources().getString(R.string.detail_information_edit_post));
popupMenu.getMenu().add(Menu.NONE, TWO, Menu.NONE, getResources().getString(R.string.detail_information_remove_post));
popupMenu.setOnMenuItemClickListener(this);
popupMenu.show();
Run Code Online (Sandbox Code Playgroud)