PopupWindow在Android7.0系統(tǒng)中顯示位置錯(cuò)誤
Android7.0之前针炉,在指定位置彈出popupwindow可以用showAsDropDown(View anchor, int xoff, int yoff)挠他,showAtLocation(View parent, int gravity, int x, int y)。但在android7.0上篡帕,用showAsDropDown()在popupwindow為全屏?xí)r殖侵,會(huì)有彈出位置異常情況,需用showAtLocation()才能正常顯示:
一镰烧、if(Build.VERSION.SDK_INT <24)
{
dropListPopupWindow.showAsDropDown(this,0,5);
}else{// 適配 android 7.0int[] location =newint[2];
getLocationOnScreen(location);intx = location[0];inty = location[1];
Log.e(getClass().getSimpleName(),"x : "+ x +", y : "+ y);
dropListPopupWindow.showAtLocation(this, Gravity.NO_GRAVITY,0, y + getHeight() +5);
}
二拢军、我們需要重寫popWindows的showAsDropDown方法:
@OverridepublicvoidshowAsDropDown(View anchor) {if(Build.VERSION.SDK_INT >=24) {? ? ? ? ? ? Rect rect =newRect();? ? ? ? ? ? anchor.getGlobalVisibleRect(rect);inth = anchor.getResources().getDisplayMetrics().heightPixels - rect.bottom;? ? ? ? ? ? setHeight(h);? ? ? ? }super.showAsDropDown(anchor);? ? }
我建議第二個(gè)比較好