Popwindow在Android 7.x系统上,当其高度为match_parent时,其位置显示为全屏了,下面给出解决方案。
// Android 7.x中,PopupWindow高度为match_parent时,会出现兼容性问题,需要处理兼容性
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
// 记录anchor在屏幕中的位置
int[] location = new int[2];
anchor.getLocationOnScreen(location);
int offsetY = location[1] + anchor.getHeight();
// Android 7.1中,PopupWindow高度为 match_parent 时,会占据整个屏幕, 故而需要在 Android 7.1上再做特殊处理
if (Build.VERSION.SDK_INT == Build.VERSION_CODES.N_MR1) {
// 获取屏幕高度
int screenHeight = DeviceUtil.getWindowHeight(context);
// 重新设置 PopupWindow 的高度
popupWindow.setHeight(screenHeight - offsetY);
}
popupWindow.showAtLocation(anchor, Gravity.NO_GRAVITY, 0, offsetY);
} else {
popupWindow.showAsDropDown(anchor);
}