setBackgroundDrawable,某些系統版本會保留默認邊距。setWidth/Height(WRAP_CONTENT) 配合某些佈局會導致測量偏差。showAsDropDown(anchor) 在 API 24+ 行為變更,若 anchor 寬度不足會偏移。PopupWindow popup = new PopupWindow(contentView,
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT, true);
// 關鍵:設置背景(哪怕透明也要設置)
popup.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
// 可選:精確控制偏移
popup.showAsDropDown(anchor, xOffset, yOffset, Gravity.START | Gravity.TOP);
若上述無效,可繼承 PopupWindow 重寫 getMaxAvailableHeight / onMeasure,強制去除系統預留邊距。但 90% 情況設置透明背景即可解決。