打开对话框正好在单击项目上方,如上下文菜单

时间:2015-07-13 05:08:58

标签: android android-listview android-dialog

我已经创建了自定义对话框,并希望将其绑定到我的列表项。 我希望这个对话框在长按列表项时表现得像上下文菜单。

换句话说,我不希望此对话框出现在屏幕的中央,而是显示在列表中的项目附近。

我花了很多时间搜索,但遗憾的是没有结果。有一些好的解决方案吗?

2 个答案:

答案 0 :(得分:4)

   btn.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub

        // infalte menu

        PopupMenu popup = new PopupMenu(activity, v);
        popup.getMenuInflater().inflate(R.menu.clipboard_popup,
                popup.getMenu());
        popup.show();
        popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {

                switch (item.getItemId()) {
                case R.id.install:

                    // Or Some other code you want to put
                    // here.. This is just an example.
                    Toast.makeText(
                            activity,
                            " Install Clicked at position " + " : "
                                    + position, Toast.LENGTH_LONG)
                            .show();

                    break;
                case R.id.addtowishlist:

                    Toast.makeText(
                            activity,
                            "Add to Wish List Clicked at position "
                                    + " : " + position,
                            Toast.LENGTH_LONG).show();

                    break;

                default:
                    break;
                }

                return true;
            }
        });

    }
});

使用自定义布局更新了答案

 PopupWindow popupwindow_obj = popupDisplay();
 popupwindow_obj.showAsDropDown(clickbtn, -40, 18); 

 // where u want show on 
 //view click event popupwindow.showAsDropDown(view, x, y);

    public PopupWindow popupDisplay() 
    { 

        final PopupWindow popupWindow = new PopupWindow(this);

          // inflate your layout or dynamically add view
            LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

            View view = inflater.inflate(R.layout.mylayout, null);

            Button item = (LinearLayout) view.findViewById(R.id.button1);

            popupWindow.setFocusable(true);
            popupWindow.setWidth(WindowManager.LayoutParams.WRAP_CONTENT);
            popupWindow.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
            popupWindow.setContentView(view);

            return popupWindow;
        }

mylayout.xml

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Window test" />
    </LinearLayout>

答案 1 :(得分:1)

如果你知道你希望对话框出现的位置,你可以这样做(注意:X:左 - &gt;右边和Y:顶部 - &gt;底部)

 AlertDialog dialog = builder.create();
     dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
     WindowManager.LayoutParams abc= dialog.getWindow().getAttributes();

 abc.gravity = Gravity.TOP | Gravity.LEFT;
 abc.x = 100;   //x position
 abc.y = 100;   //y position

 dialog.show();
相关问题