How to show PopupWindow at special location?

后端 未结 3 914
醉梦人生
醉梦人生 2020-11-30 19:31

I need to show PopupWindow under one Views shown on the screen.

How can I calculate coordinates of needed View and place

3条回答
  •  一个人的身影
    2020-11-30 20:13

    To get size of the main application screen without stuff like title and notification bars, override the following method in the class generating the screen in question (sizes are measured in pixels):

    @Override
    protected void onMeasure (int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int height = MeasureSpec.getSize(heightMeasureSpec);
    }
    

    To get the bottom coordinate of the view under which you want to show the popup:

    View upperView = ...
    int coordinate = upperView.getBottom();
    

    Now as long as height - coordinate is large enough for your popup view, you can simply place the popup like this:

    PopupWindow popup = new PopupWindow();
    
    Button button = new Button(this);
    button.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            popup.showAtLocation(parent, Gravity.CENTER, 0, coordinate);
        }
    });
    

    Here, showAtLocation() takes the parent view as an argument together with gravity and location offsets.

提交回复
热议问题