如何使PopupWindow居中显示?

3

我创建了一个Activity,在其中添加了一个按钮,当点击按钮时会弹出一个弹窗。下面是showPopup()方法的代码:

    private void showPopup() {
    LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    View layout = inflater.inflate(R.layout.popup_layout, (ViewGroup) findViewById(R.id.popup_element), false);

    final PopupWindow pwindo = new PopupWindow(layout, LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT, true);

    Button btnAgree = (Button) layout.findViewById(R.id.ok);
    btnAgree.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            pwindo.dismiss();
        }
    });

    pwindo.showAtLocation(layout, Gravity.CENTER, 0, 0);
    }
}

我希望将其垂直和水平居中。我尝试了一些在SO上看到的方法,但没有一个有效。为什么弹出窗口总是在屏幕顶部?


new PopupWindow(layout, LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT, true); 这个不起作用吗? - Pedro Oliveira
不,它适合屏幕但不是居中的,它从顶部开始并在屏幕中间结束。 - smartmouse
2
尝试将你的popup_layout改为match_parent视图,然后在里面添加居中的视图。你可以使用FrameLayout来实现这个效果。 - Pedro Oliveira
这就是诀窍!谢谢。 - smartmouse
2个回答

4

您可以使用setHorizontalOffset:

ListPopupWindow popupWindow = new ListPopupWindow(this);

// Position
popupWindow.setAnchorView(btn);
popupWindow.setPromptPosition (ListPopupWindow.POSITION_PROMPT_ABOVE);
popupWindow.setHorizontalOffset((btn.layoutParams.width - popupWindow.getWidth())/2);

enter image description here


0

这段代码将在您指定的视图中心显示完美的弹出窗口

// click event of specific view
  @Override
    public void onClick(View v) {
        super.onClick(v);
       if (v == lin_filterActionbar) {
            PopupWindow popupwindow_obj; // create object
            popupwindow_obj = dialog_AllergiesSelect(RR_HomeActivity.this,lin_filterActionbar);
            popupwindow_obj.showAsDropDown(lin_filterActionbar);

        } 
    }



 private PopupWindow dialog_AllergiesSelect(Context context,LinearLayout lin) {

        final PopupWindow dialog_AllergiesSelect = new PopupWindow(this);

        View v = View.inflate(context, R.layout.dialog_filter_actionbar, null);
        dialog_AllergiesSelect.setContentView(v);
        dialog_AllergiesSelect.setWidth(WindowManager.LayoutParams.WRAP_CONTENT);
        dialog_AllergiesSelect.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
        dialog_AllergiesSelect.setFocusable(true);

//        TextView lis_testSelection = (TextView) v.findViewById(R.id.dialogfilter_txt_filter);
//        LinearLayout.LayoutParams lin_laLayoutParams = new LinearLayout.LayoutParams((int) (sunApplication.getDeviceWidth() - (sunApplication.getDeviceScale() * dialog_width_dvide_allerges_spinner_width)), (int) (sunApplication.getDeviceHeight() - (sunApplication.getDeviceScale() * dialog_width_dvide_allerges_height)));
//        lis_testSelection.setLayoutParams(lin_laLayoutParams);

        // Some offset to align the popup a bit to the right, and a bit down, relative to button's position.
        int OFFSET_X = 0;
        int OFFSET_Y = 0;

        // Clear the default translucent background
        dialog_AllergiesSelect.setBackgroundDrawable(new BitmapDrawable());

        // Displaying the popup at the specified location, + offsets.
        //dialog_AllergiesSelect.showAtLocation(v, Gravity.C, p.x + OFFSET_X, p.y + OFFSET_Y);

        Rect location = locateView(lin);

        dialog_AllergiesSelect.showAtLocation(v, Gravity.TOP|Gravity.CENTER, 0, location.bottom);

        // Getting a reference to Close button, and close the popup when clicked.

        return dialog_AllergiesSelect;

    }
    public static Rect locateView(View v)
    {
        int[] loc_int = new int[2];
        if (v == null) return null;
        try
        {
            v.getLocationOnScreen(loc_int);
        } catch (NullPointerException npe)
        {
            //Happens when the view doesn't exist on screen anymore.
            return null;
        }
        Rect location = new Rect();
        location.left = loc_int[0];
        location.top = loc_int[1];
        location.right = location.left + v.getWidth();
        location.bottom = location.top + v.getHeight();
        return location;
    }

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