美文网首页
针对7.0以上手机 PopupWindow显示位置不正确问题处理

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

作者: 坏小子_嘟嘟 | 来源:发表于2018-07-04 17:35 被阅读13次

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

相关文章

网友评论

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

      本文链接:https://www.haomeiwen.com/subject/owcyuftx.html