PopupWindow 先顯示,隨後將 ImageView 設為 VISIBLE,結果 ImageView 被 PopupWindow 遮蓋 (PopupWindow 在上層)。
PopupWindow 預設繪製在 **DecorView** (Window 根視圖) 之上,屬於獨立視圖層級,高於普通 View (包括後來顯示的 ImageView)。
showAsDropDown(anchorView),PopupWindow 會自動定位在 anchorView 正下方,且 **Z-Order 低於後續添加的同層 View** (視 Android 版本行為略有不同,但通常符合預期)。popupWindow.showAtLocation(parentView, Gravity.NO_GRAVITY, x, y) 並在 parentView 層級管理。setOverlayMode(false) (預設即為 false),可讓 PopupWindow 不處於覆蓋模式,但仍受 WindowManager 控制。// 1. 先確保 ImageView 可見
imageView.setVisibility(View.VISIBLE);
// 2. 準備 PopupWindow
View popupView = LayoutInflater.from(context).inflate(R.layout.popwindow, null);
PopupWindow popupWindow = new PopupWindow(popupView,
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT,
true); // focusable = true 允許外部點擊關閉
// 3. 設定背景 (否則某些版本無動畫/無法點擊外部關閉)
popupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
// 4. 顯示在 ImageView 正下方,自動對齊
popupWindow.showAsDropDown(imageView, 0, 10); // xOffset=0, yOffset=10dp
// 5. (可選) 監聽 dismiss 事件
popupWindow.setOnDismissListener(() -> {
// 例如恢復 ImageView 狀態
});
// 取得 ImageView 在螢幕上的絕對座標
int[] location = new int[2];
imageView.getLocationOnScreen(location);
int x = location[0];
int y = location[1] + imageView.getHeight() + dpToPx(10); // 下方 10dp
popupWindow.showAtLocation(rootView, Gravity.NO_GRAVITY, x, y);
setVisibility(VISIBLE),再 showAsDropDown。WRAP_CONTENT 需 PopupView 根佈局有確定尺寸或 measure 過;若尺寸不確定,可先 popupWindow.setWidth(dpToPx(200)) 等。setBackgroundDrawable 否則 focusable=true 時外部點擊無法關閉,且某些版本無動畫。