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

今天给大家分享下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
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值