解决PopupWindow在7.0以上位置不正确的bug

来源:互联网 发布:贵金属交易软件排行 编辑:程序博客网 时间:2024/05/21 10:32

我们在开发过程中会经常使用PopupWindow,但在7.0使用PopupWindow的showAsDropDown方法会出现无依附效果异常,这是什么问题呢?其实原因在于PopupWindow内部出现了BUG,google大牛在发布时候忘记修改测试时代码导致的(只是个人估计),至于以后用PopupWindow都需要对7.0做下处理吗?这个问题嘛我也不清楚

注:只有Build.VERSION.SDK_INT == 24存在这个BUG,7.1已经此BUG已经修复了,只对7.0做下处理就行

解决PopupWindow7.0依附问题有以下俩种解决方法;

方法一:重写showAsDropDown(view)

public class SupportPopupWindow extends PopupWindow {

    public SupportPopupWindow(View contentView, int width, int height) {        super(contentView, width, height);    }    @Override    public void showAsDropDown(View anchor) {        if (Build.VERSION.SDK_INT == 24) {            Rect rect = new Rect();            anchor.getGlobalVisibleRect(rect);            int h = anchor.getResources().getDisplayMetrics().heightPixels - rect.bottom;            setHeight(h);        }        super.showAsDropDown(anchor);    }    @Override    public void showAsDropDown(View anchor, int xoff, int yoff) {        if (Build.VERSION.SDK_INT == 24) {            Rect rect = new Rect();            anchor.getGlobalVisibleRect(rect);            int h = anchor.getResources().getDisplayMetrics().heightPixels - rect.bottom;            setHeight(h);        }        super.showAsDropDown(anchor, xoff, yoff);    }}

方法二:依附方法对build=24做下修改

mView是依附控件的ID

if (Build.VERSION.SDK_INT == 24) {            // 获取控件的位置,安卓系统=7.0            int[] location = new int[2];            mView.getLocationOnScreen(location);            int x = location[0];            int y = location[1];            L.e(getClass().getSimpleName(), "x : " + x + ", y : " + y);      window.showAtLocation(mView, Gravity.NO_GRAVITY, 0, location[1] + mView.getHeight());        } else {            window.showAsDropDown(mView);        }以上俩种方法都可以

解决问题,其实俩种方法原理都是一样的,只不过是写法不同。

再次申明一下 7.1已经对此BUG修复了,只需要对7.0做处理,最后祝程序猿们工作顺利O(∩_∩)O哈!

阅读全文
0 0
原创粉丝点击