关于PopuWindow位置问题
popuwindow在Android 版本7.0以上,使用showAsDropDown会发现显示的位置不在并不在指定控件下面。
那么在Android版本7.0以上,我们就需要去获取屏幕的高度,重新设置它的显示位置。
//设置显示在哪个控件下面
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { // Android 7.x中,PopupWindow高度为match_parent时,会出现兼容性问题,需要处理兼容性
int[] mLocation = new int[2];
rl_match.getLocationInWindow(mLocation);
int offsetY = mLocation[1] + rl_match.getHeight();
if (Build.VERSION.SDK_INT >= 25) { // Android 7.1中,PopupWindow高度为 match_parent 时,会占据整个屏幕
//故而需要在 Android 7.1上再做特殊处理
int screenHeight = getActivity().getWindowManager().getDefaultDisplay().getHeight(); // 获取屏幕高度
window.setHeight(screenHeight - offsetY); // 重新设置 PopupWindow 的高度
}
window.showAtLocation(view, Gravity.NO_GRAVITY, 0, offsetY);
} else {
window.showAsDropDown(rl_match);//设置显示在哪个控件下面
}