为什么Android popupwindow showAsDropDown offsetx无效?

Haz*_*ass 3 mobile android android-layout

popupWindow.showAsDropDown(morebutton, xOffset, yOffset);
Run Code Online (Sandbox Code Playgroud)

不管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);
Run Code Online (Sandbox Code Playgroud)

无论我设置的值offsetX如何,他都位于屏幕显示的右侧

Mar*_*bey 5

PopupWindow尝试默认将您的对齐contentView到您anchorGravity.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);
Run Code Online (Sandbox Code Playgroud)

您可能想要类似的东西:

popupWindow.showAsDropDown(morebutton, xOffset - 220, -40);
Run Code Online (Sandbox Code Playgroud)