我在我的应用中有一些弹出窗口,它们是全屏的,并且以下是相应的代码:
content.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
content.measure(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
int screenWidth = windowManager.getDefaultDisplay().getWidth();
int screenHeight = windowManager.getDefaultDisplay().getHeight();
int x = screenWidth / 2 - content.getMeasuredWidth() / 2;
int y = screenHeight / 2 - content.getMeasuredHeight() / 2;
window.showAtLocation(content, Gravity.NO_GRAVITY, x, y);
使窗口居中显示。但我有另一个不是全屏的Activity,当弹出窗口打开时,它位于正确的下方,而不是需要的位置。
试图找出为什么会发生这种情况,我认为showAtLocation方法将其相对于当前Activity显示,但我需要相对于显示器显示。
我该怎么做?还是有更简单的方法只让弹出窗口居中显示吗?
mPw.showAtLocation(v, Gravity.BOTTOM, 0, 0);
在像 Nexus 5 这样的 xxhpi 设备上可以吗?我在这方面遇到了问题... - Huy TowerpopupWindow.ShowAtLocation(anyViewOnlyNeededForWindowToken, GravityFlags.Center, 0, 0);
- JotaPardo