Android 7.0系统 PopupWindow的bug

来源:互联网 发布:统计学数据分析案例题 编辑:程序博客网 时间:2024/05/17 22:53

起初产品经理说过一次,因为是刚入职接手的项目所以,我当时没太在意这个bug,当时报备的是华为手机有这种问题,其他机型没有,我当时就想应该是系统兼容性问题,毕竟国内系统改的乱七八糟,后来发现oppo也出现了,就是PopupWindow显示是从顶部弹出的,我看下代码,没什么问题,但是小米就没问题,后来google了一波是系统bug,于是就做兼容性处理,分分钟奏效

我们预期的效果图:

这里写图片描述

但是android 7.0却是这样:

这里写图片描述

其实原因就是android 7.0系统源码对popwindow处理存在bug,以前我接手的代码是这样的:

 private void showPopup(int position) {        View tView = mViewArray.get(selectPosition).getChildAt(0);        if (tView instanceof ViewBaseAction) {            ViewBaseAction f = (ViewBaseAction) tView;            f.show();        }        if (popupWindow.getContentView() != mViewArray.get(position)) {            popupWindow.setContentView(mViewArray.get(position));        }        popupWindow.showAsDropDown(this, 0, 0);    }

以上代码是没有对7.0做处理的。可能上一个开发还没test,就离职了,那就让我来填这个坑吧(2333)

兼容处理代码:

    private void showPopup(int position) {        View tView = mViewArray.get(selectPosition).getChildAt(0);        if (tView instanceof ViewBaseAction) {            ViewBaseAction f = (ViewBaseAction) tView;            f.show();        }        if (popupWindow.getContentView() != mViewArray.get(position)) {            popupWindow.setContentView(mViewArray.get(position));        }        //TODO Android 7.x中,PopupWindow高度为match_parent时,会出现兼容性问题,需要处理兼容性        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {            int[] location = new int[2]; // 记录anchor在屏幕中的位置            this.getLocationOnScreen(location);            int offsetY = location[1] + this.getHeight();            //TODO Android 7.1中,PopupWindow高度为 match_parent 时,会占据整个屏幕,故而需要在 Android 7.1上再做特殊处理            if (Build.VERSION.SDK_INT == Build.VERSION_CODES.N_MR1) {                int screenHeight = displayHeight; // 获取屏幕高度                popupWindow.setHeight(screenHeight - offsetY); // 重新设置 PopupWindow 的高度            }            popupWindow.showAtLocation(this, Gravity.NO_GRAVITY, 0, offsetY);        } else {            popupWindow.showAsDropDown(this);        }    }

这样就达到预期效果了。

原创粉丝点击