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,他都在屏幕右侧显示
答案 0 :(得分:5)
PopupWindow
会尝试将您的contentView
与anchor
Gravity.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);