获取弹出窗口的度量 [英] Get The Measures of Popup Window

查看:94
本文介绍了获取弹出窗口的度量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经设置了弹出窗口,但是我想将其置于按钮(视图v)下方的中心,需要单击以将其打开:

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, View v){
    int[] location = new int[2];
    v.getLocationOnScreen(location);

    ViewGroup base = (ViewGroup) getView().findViewById(R.id.pup_pattern);  
    LayoutInflater inflater = (LayoutInflater) c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View pupLayout = inflater.inflate(R.layout.linearlayout_popup, base);

    final PopupWindow pup = new PopupWindow(pupLayout, android.view.ViewGroup.LayoutParams.WRAP_CONTENT, android.view.ViewGroup.LayoutParams.WRAP_CONTENT);

    int x = location[0] - (int) ((pupLayout.getWidth() - v.getWidth()) / 2 ); // --> pupLayout.getWidth() gives back -2?
    int y = location[1] + v.getHeight() + 10;

    pup.setFocusable(true);
    pup.showAtLocation(v, Gravity.NO_GRAVITY, x, y);
}

有人有采取措施的想法吗?

Has anybody an idea to get measures?

推荐答案

您将无法获得尚未在屏幕上绘制的视图的高度或宽度.

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

pupLayout.getWidth()//这将为您提供0

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

您需要获得这样的宽度

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

像这样使用它

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

这篇关于获取弹出窗口的度量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆