Get The Measures of Popup Window

后端 未结 2 1313
深忆病人
深忆病人 2020-12-15 07:09

I already set up the pop up window, but i want to center it below the button (View v), that needs to be clicked to open it:

public void showPopup(Context c,          


        
相关标签:
2条回答
  • 2020-12-15 07:24

    Another way:

    /**
     * @return Point object which:<br>
     * point.x : contains width<br>
     * point.y : contains height
     */
    public static Point getViewSize(View view) {
        Point size = new Point();
        view.post(new Runnable() {
            @Override
            public void run() {
                size.set(view.getWidth(), view.getHeight());
            }
        });
        return size;
    }
    
    0 讨论(0)
  • 2020-12-15 07:35

    You won't get the height or width of the view, which hasn't been drawn on the screen.

    pupLayout.getWidth() // this will give you 0

    You need to get the width like this

    int width = MeasureSpec.makeMeasureSpec(0, MeasureSpec. UNSPECIFIED);
    

    Use it like this

    View pupLayout = inflater.inflate(R.layout.linearlayout_popup, base);
    pupLayout.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), 
                MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
    
    int x = location[0] - (int) ((pupLayout.getMeasuredWidth() - v.getWidth()) / 2 );
    
    0 讨论(0)
提交回复
热议问题