在顶部弹出显示

时间:2017-03-21 16:33:23

标签: java android

我刚刚创建了一个简单的弹出列表项,但是当我点击Grid项时,它会显示页面顶部的弹出窗口,如enter image description here所有我需要的东西我想在下面显示弹出窗口项目网格 这是我的onClick

gridView1.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
    @Override
    public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
        //Creating the instance of PopupMenu
        PopupMenu popup = new PopupMenu(getActivity(), gridView1);
        //Inflating the Popup using xml file
        popup.getMenuInflater().inflate(R.menu.popup_menu, popup.getMenu());
        //registering popup with OnMenuItemClickListener
        popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                Toast.makeText(
                        getActivity(),
                        "You Clicked : " + item.getTitle(),
                        Toast.LENGTH_SHORT
                ).show();
                return true;
            }
        });

        popup.show(); //showing popup menu
        return true;

    }
});

这是我的menu.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android" >

    <item
        android:id="@+id/newCheck"
        android:title="New Check"/>

    <item
        android:id="@+id/settleCheck"
        android:title="Settle Check"/>

    <item
        android:id="@+id/newGuestCheck"
        android:title="New guest Check"/>

    <item
        android:id="@+id/discountCheck"
        android:title="Discount Check"/>

    <item
        android:id="@+id/printCheck"
        android:title="Print Check"/>

</menu>

1 个答案:

答案 0 :(得分:1)

你应该改变这一行

PopupMenu popup = new PopupMenu(getActivity(), gridView1);

到这个

PopupMenu popup = new PopupMenu(getActivity(), view);

所以它会将菜单的锚点设置为正确的元素。