android7.0及以上popwindow显示位置异常的原因

原本据说这事sdk24 25 带的一个bug   但是延伸到了 8.0就有点奇怪了  

网上搜到的方法大部分是

if (Build.VERSION.SDK_INT >= 24) {
     int[] location = new int[2];
     anchor.getLocationOnScreen(location);
     // 7.1 版本处理
     if (Build.VERSION.SDK_INT == 25) {
         WindowManager windowManager = (WindowManager) pw.getContentView().getContext().getSystemService(Context.WINDOW_SERVICE);
         if (windowManager != null) {
             int screenHeight = windowManager.getDefaultDisplay().getHeight();
             // PopupWindow height for match_parent, will occupy the entire screen, it needs to do special treatment in Android 7.1
             pw.setHeight(screenHeight - location[1] - anchor.getHeight() - yoff);
         }
     }
     pw.showAtLocation(anchor, Gravity.NO_GRAVITY, xoff, location[1] + anchor.getHeight() + yoff);

 } else {
     pw.showAsDropDown(anchor, xoff, yoff);
 }

简单来说就是 通过测量view在window的位置 然后测定偏移量 然后显示出来 但是测量后  你会发现在 Android 8.0手机上 会无效  

但是思路可以借鉴啊   

于是有人研究出第二种方法 既然测量windows位置不准确 那就直接测量view在屏幕上的像素位置 然后运算得出相应的需要显示位置

 if (Build.VERSION.SDK_INT >= 24) {
            Rect visibleFrame = new Rect();
            anchor.getGlobalVisibleRect(visibleFrame);
            int height = anchor.getResources().getDisplayMetrics().heightPixels - visibleFrame.bottom;
            pw.setHeight(height);
            pw.showAsDropDown(anchor, xoff, yoff);
        } else {
            pw.showAsDropDown(anchor, xoff, yoff);
        }

亲测有效 

原文地址点击打开链接


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值