为什么android弹出窗口显示AsDropDown offsetx无效


popupWindow.showAsDropDown(morebutton, xOffset, yOffset);

无论xOffset的值如何,弹出窗口都在屏幕显示的右侧

final PopupWindow popupWindow = new PopupWindow(DashboardActivity.applicationContext);
                        LayoutInflater inflater = (LayoutInflater) DashboardActivity.applicationContext.getSystemService(
                                Context.LAYOUT_INFLATER_SERVICE);
popupWindow.setFocusable(true);
                        popupWindow.setWidth(WindowManager.LayoutParams.WRAP_CONTENT);
                        popupWindow.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
                        popupWindow.setContentView(view);
                        popupWindow.setBackgroundDrawable(new ColorDrawable(
                                android.graphics.Color.TRANSPARENT));
popupWindow.showAsDropDown(morebutton, **-220**, -40);

无论我设置什么值 offsetX,他都在屏幕显示的右侧

默认情况下,

PopupWindow尝试将contentViewanchorGravity.TOP | Gravity.START对齐(左上角(。如果没有空间留给您的contentView它会偏移以适应屏幕,这可能就是它留在您的右侧的原因。

以下 xOffset 将contentView对齐anchor的右上角:

view.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
int xOffset = -(view.getMeasuredWidth() - morebutton.getWidth());
popupWindow.showAsDropDown(morebutton, xOffset, 0);

您可能需要类似以下内容:

popupWindow.showAsDropDown(morebutton, xOffset - 220, -40);

最新更新