Android 不同版本7.0以下,7.0,7.1以上 popwindow展示位置控制分享

来源:互联网 发布:激战2母夏尔捏脸数据 编辑:程序博客网 时间:2024/06/06 05:36

今天给大家分享下Android不同版本下popwindow展示位置控制;公司的项目在进行云测后发现了popwindow适配出现问题;

下面先给大家展示下我之前有问题代码:

if (Build.VERSION.SDK_INT >=24) {    // 系统7.0以上, popupwindow底层修改了Gravity属性 Gravity.START    // | Gravity.TOP    int[] a = new int[2];    anchor.getLocationOnScreen(a);    showAtLocation(activityWeakReference.get().getWindow().getDecorView(),            Gravity.NO_GRAVITY, 0, a[1] + anchor.getHeight());} else {    CardsListPanel.super.showAsDropDown(anchor);}

上面代码的意思就是在Android系统7.0以下时就调用popwindow.showAsDropDown(anchor)方法;

7.0以上的就调用:

showAtLocation(activityWeakReference.get().getWindow().getDecorView(),            Gravity.NO_GRAVITY, 0, a[1] + anchor.getHeight());
设置显示的样式,以及popwindow展示的起始位置anchor到屏幕的高度+anchor自身的高度;

我们看下效果图:

7.0以下和7.0的界面是正常的,可是7.1以上的就尴尬了;



7.1以上的界面:



界面上面的搜索框直接被遮盖住了;查了百度,上面千篇一律都是说要像上面那样写;

搞了半天才查到可行的方法,这里大家分享下,把代码改成下面这样:

if (Build.VERSION.SDK_INT >=24) {    // 系统7.0以上, popupwindow底层修改了Gravity属性 Gravity.START    // | Gravity.TOP    int[] a = new int[2];    anchor.getLocationOnScreen(a);    // 7.1以上 版本处理    if (Build.VERSION.SDK_INT >= 25) {        //note!Gets the screen height without the virtual key        WindowManager wm = (WindowManager) getContentView().getContext().getSystemService(activity.WINDOW_SERVICE);        int screenHeight = wm.getDefaultDisplay().getHeight();        /*        * PopupWindow height for match_parent,        * will occupy the entire screen, it needs to do special treatment in Android 7.1         */        setHeight(screenHeight - a[1] - anchor.getHeight());    }    showAtLocation(activityWeakReference.get().getWindow().getDecorView(),            Gravity.NO_GRAVITY, 0, a[1] + anchor.getHeight());} else {    CardsListPanel.super.showAsDropDown(anchor);}
大家看到差别在哪里了吗?代码意思改为,7.0以上改成showAtLocation()方法去展示位置,但7.1以上版本要先重新设置popwindow的高度,之后再设置展示的位置;到这里就ok了,我试了下7.1.1和8.0的手机都没问题了;展示效果都是好的。



希望本文对大家有所帮助!!!



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