安卓 ListView 中的弹出式菜单问题

8

我是一个新手,正在创建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;
            }
        });
    }

它产生了这个输出

enter image description here

2] 使用ContextMenu显示以下输出

我们可以在ContextMenu中保持宽度和高度,但它总是显示在中心而不是每一行的Listview数据

enter image description here

但我想要下面图像类型的弹出菜单宽度和高度很小

enter image description here

请为此提供解决方案。


也许这个答案对你有帮助:https://dev59.com/XGEh5IYBdhLWcg3wjD_n#22311108 - curiousMind
5个回答

2
你可以使用ListPopupWindow来实现你想要的功能。您可以将更多选项图标或菜单图标设置为弹出窗口的锚点。
ListPopupWindow mListPopupWindow;
mListPopupWindow = new ListPopupWindow(this, null);
mListPopupWindow.setWidth(300);
mListPopupWindow.setAnchorView(menuIcon);
mListPopupWindow.setHeight(200);
mListPopupWindow.setAdapter(yourAdapter);
mListPopupWindow.show();

您将获得您想要的东西。


你遇到了什么问题? - frogEye
它显示在中心,我想让它动态化? - Harshad
你一定做错了什么。它会显示在你指定的锚点上。如果我能管理,我会为你发布可工作的代码。 - frogEye

2

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

这里的ONE和TWO显示了您在弹出菜单中提供的选项的索引。例如,在第一个位置是编辑文章,在第二个位置是删除文章等。

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();

找不到 ONE 和 TWO,并且 popupMenu.setOnMenuItemClickListener(this); 给我报错。 - Harshad
ONE和TWO显示选项菜单的索引,您可以在ONE和TWO的位置设置int数字,而popupMenu.setOnMenuItemClickListener(this)会出现错误,因为您没有在活动中实现PopupMenu.OnMenuItemClickListener,或者您可以简单地将其替换为此一个popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener())。 - Bhavnik
抱歉,我无法理解您的意思。 - Harshad
我尝试使用popupMenu.setOnMenuItemClickListener(this);这行代码,但是它给我显示红色信号。 - Harshad

2
如果您正在使用适配器,可以将其放置在适配器的getView(...)方法中。
    imvMore.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            showPopupMenu(act,v);
        }
    });

以及put方法

private void showPopupMenu(Activity act, View view){
    PopupMenu popupMenu = new PopupMenu(act,view);
    popupMenu.getMenuInflater().inflate(R.menu.menu_popup, popupMenu.getMenu());
    popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
        @Override
        public boolean onMenuItemClick(MenuItem item) {
            switch (item.getItemId()) {

            }
            return true;
        }
    });
    popupMenu.show();
}

在你的适配器类中。

注意:act是创建构造函数适配器时必须绑定的活动,例如:

public YourAdapter(Activity act, ArrayList<ItemOfYourModel> data){
    this.data = data;
    this.act = act;
}

在Activity中,您可以编写以下代码:
ArrayList<ItemOfYourModel> listData = new ArrayList<ItemOfYourModel>();
listData.add(new YourItemOfYourModel(...));
YourAdapter adapter = new YourAdapter(this,listData);

请详细描述如何在适配器中绑定操作? - Harshad
@Harshad,你使用了哪个适配器和活动?你使用了ListView还是RecyclerView? - dolphin
ListView和BaseAdapter - Harshad
@Harshad,请看我的新更新答案。希望能帮到你。 - dolphin

1

Google Play使用Holo主题来创建PopupMenu。您可以通过在styles.xml中创建自定义的style来实现相同的效果:

<style name="PopupMenuStyle" parent="android:Theme.Holo.Light">
        <!-- Your custom attributes must be put here. This is optional, but the parent must not be changed -->
</style>

然后在您的代码中更改PopupMenu的样式:
Context wrapper = new ContextThemeWrapper(this, R.style.PopupMenuStyle);
PopupMenu popupMenu = new PopupMenu(wrapper,view);

我使用了这个,但是菜单显示在中心,就像上下文菜单一样。我希望它能够在ListView的每一行中显示。 - Harshad
你必须在整个视图上进行设置。相反,像Google Play在列表项中一样添加一个按钮。然后它就不会出现在中心位置了。 - Piyush
如何设置宽度和高度是我第一个问题的条件。 - Harshad
对于PopupMenu,你不能这样做。你在菜单中看到的每个项目的白色背景是一个固定大小的可绘制对象。这个大小是菜单的理想大小,但如果你仍然想改变大小,可以使用PopupWindow。 - Piyush

1
我认为最好的解决方案是使用PopupWindow,您可以控制它的每一个细节,并且创建您想要的菜单非常容易。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接