PopupWindow在Android7.0系统中显示位置错误
Android7.0之前,在指定位置弹出popupwindow可以用showAsDropDown(View anchor, int xoff, int yoff),showAtLocation(View parent, int gravity, int x, int y)。但在android7.0上,用showAsDropDown()在popupwindow为全屏时,会有弹出位置异常情况,需用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); }
我建议第二个比较好
网友评论