How to center PopupWindow?

2019-02-28 15:03发布

I created an Activity in which i add a button that throws a popup when is clicked. Here is the code of showPopup() method:

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

I would center it both vertically and orizzontally. I tried several ways that i see here on SO but none worked. Why i always get the popup window at the top of the screen?

2条回答
Explosion°爆炸
2楼-- · 2019-02-28 15:40

this code will show perfect popup in center of you specific view

// 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;
    }
查看更多
我欲成王,谁敢阻挡
3楼-- · 2019-02-28 15:52

you can use 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

查看更多
登录 后发表回答