android - 获取弹窗的度量

标签 android popupwindow measurement

我已经设置了弹出窗口,但我想将它置于按钮(View v)下方,需要点击才能打开它:

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

有人想采取措施吗?

最佳答案

你不会得到 View 的高度或宽度,它还没有在屏幕上绘制。

pupLayout.getWidth()//这会给你 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 );

关于android - 获取弹窗的度量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15862052/

相关文章:

android - AppCompat 工具栏显示不需要的设置按钮

Swift - 测量转换(到 :) miles to feet gives wrong result

c - movndq 有效吗?

python - 用python测量图片中物体之间的距离

php - Ajax 弹出窗口

java - 如何使用 Java 处理 Selenium WebDriver 中的弹出窗口

android - SeTransactionSyncTask : Error retrieving account java. lang.IllegalStateException:没有当前的点击支付帐户

android - eclipse 中外部库的使用

php - 我可以使用 Manifest 或 Appcache 文件定位特定的浏览器/设备,或者只是从桌面浏览器中排除 appcache 吗?

android - PopupWindow 中 ListView 的问题