Android 版本兼容问题集锦(1)

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);    }

我建议第二个比较好

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容