CompatPopupWindow
優化PopupWindow在屏幕右邊或下邊的時候顯示不全的問題.
-
提供一套AnimationStyle.
調用showAsDropDown時,如果anchor太靠下,可能空間不夠,造成PopupWindow顯示不全,所以要判斷進行y方向偏移。靠右的畫gravity可以用
Gravity.END|Gravity.Top
解決。
核心代碼
@Override
public void showAsDropDown(View anchor, int xoff, int yoff, int gravity) {
int adjustYoff = adjustYoff(anchor, yoff);
super.showAsDropDown(anchor, xoff, adjustYoff, gravity);
}
private int adjustYoff(View anchor, int yoff) {
if (anchor == null) {
return 0;
}
anchor.getLocationInWindow(location);
int availableHeight = UtilsSize.getScreenHeight(anchor.getContext()) - location[1] - anchor.getHeight();
int minWidowHeight = getMinWidowHeight();
if (availableHeight < minWidowHeight && minWidowHeight != -1) {
yoff -= minWidowHeight;
if (useAnimStyle) {
setAnimationStyle(R.style.AnimStyle_Bottom);
}
} else {
if (useAnimStyle) {
setAnimationStyle(R.style.AnimStyle);
}
}
return yoff;
}