针对7.0以上手机 PopupWindow显示位置不正确问题处理

public class MyPopupWindow extends PopupWindow {

public MyPopupWindow(Context context) { super(context); }

public MyPopupWindow(Context context, AttributeSet attrs) { super(context, attrs); }

public MyPopupWindow(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); }

public MyPopupWindow(View contentView) { super(contentView); }

public MyPopupWindow(int width, int height) { super(width, height); }

public MyPopupWindow(View contentView, int width, int height) { super(contentView, width, height); }

/** * 在android7.0上,如果不主动约束PopuWindow的大小,比如,设置布局大小为 MATCH_PARENT,那么PopuWindow会变得尽可能大, * 以至于 view下方无空间完全显示PopuWindow,而且view又无法向上滚动,此时PopuWindow会主动上移位置,直到可以显示完全。 *  解决办法:主动约束PopuWindow的内容大小,重写showAsDropDown方法: * * @param anchor */

@Override

public void showAsDropDown(View anchor) { if (Build.VERSION.SDK_INT >= 24) { Rect visibleFrame = new Rect(); anchor.getGlobalVisibleRect(visibleFrame); int height = anchor.getResources().getDisplayMetrics().heightPixels - visibleFrame.bottom; setHeight(height); } super.showAsDropDown(anchor); }}

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

推荐阅读更多精彩内容