Get The Measures of Popup Window

不打扰是莪最后的温柔 提交于 2019-11-29 01:25:33
Pragnani

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

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;
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!