如何在点击的项目上方准确打开对话框,就像上下文菜单一样。

5

我创建了自定义对话框并希望将其绑定到我的列表项。当长按列表项时,我希望这个对话框的行为类似于上下文菜单。

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

我花了很多时间搜索方法,但不幸的是没有结果。有没有什么好的解决方案?


1
使用“PopupWindow”可以帮助您在视图上显示窗口。 - Hiren Dabhi
谢谢,我会去搜索它。 - YFeizi
http://rajeshandroiddeveloper.blogspot.in/2013/07/android-popupwindow-example-in-listview.html - Anoop M Maddasseri
2个回答

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

我不想使用菜单!我有一个包含3个按钮的自定义布局,并且是水平方向的。 - YFeizi
我的问题在这里:popup.getMenuInflater().inflate(R.menu.clipboard_popup, popup.getMenu()); - YFeizi
这不是菜单!它是一个布局。 - YFeizi
太好了,现在它可以工作了 :) 我需要稍微调整一下来获得更好的位置,谢谢 :) - YFeizi

2

如果您知道您想要对话框出现的位置,可以像下面这样操作(注:X表示从左到右,Y表示从上到下)

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

我不知道如何计算列表视图中所点击项的位置:/ - YFeizi
2
你有listview的ID。检查它的参数。这是onItemClick方法的第4个参数。 - Mirza

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