遇到的坑,版本兼容问题集锦

作者: 网络编程  发布:2019-09-08

PopupWindow 的height 使用 match_parentfill_parent致使的难点

PopupWindow在Android7.0系统中显得地点不当

主题素材陈述

api >=24(Android 7.0)时 View anchor 相对于anchor popupwindow 的岗位 无效果与利益 效果是全屏

 public void showAsDropDown(View anchor) public void showAsDropDown(View anchor, int xoff, int yoff) public void showAsDropDown(View anchor, int xoff, int yoff, int gravity) public void showAtLocation(View parent, int gravity, int x, int y)

代码如下:

button.setOnClickListener(new View.OnClickListener() { @Override public void onClick { PopupWindow popupWindow = new PopupWindow(view, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT); popupWindow.setOutsideTouchable; popupWindow.setFocusable; popupWindow.showAsDropDown; } });

Android7.0事先,在钦点地方弹出popupwindow能够用showAsDropDown(View anchor, int xoff, int yoff),showAtLocation(View parent, int gravity, int x, int y)。但在android7.0上,用showAsDropDown()在popupwindow为全屏时,会有弹出位置卓殊情状,需用showAtLocation()本事符合规律呈现:

效果与利益如下

图片 1![popupwindow_24.png]()

消除措施

 if (Build.VERSION.SDK_INT < 24){ popupWindow.showAsDropDown; } else { // 适配 android 7.0 int[] location = new int[2]; button.getLocationOnScreen; int x = location[0]; int y = location[1]; popupWindow.showAtLocation(button, Gravity.NO_GRAVITY, x,y+button.getHeight; }

以上代码解决全屏问题

图片 2popupwindow-2.png

本身擦 android 7.0 难题化解了,google android 7.1 出来了 MB 难题又来了

图片 3popupwindow_25.png

聊起底化解办法

 if (Build.VERSION.SDK_INT < 24) { popupWindow.showAsDropDown; } else { int[] location = new int[2]; // 获取控件在屏幕的位置 button.getLocationOnScreen; if (Build.VERSION.SDK_INT == 25) { int tempheight = popupWindow.getHeight(); if (tempheight == WindowManager.LayoutParams.MATCH_PARENT || screenHeight <= tempheight) { popupWindow.setHeight(screenHeight - location[1] - button.getHeight; } } popupWindow.showAtLocation(button, Gravity.NO_GRAVITY, location[0], location[1] + button.getHeight; }

github大家有何样解决办法???

一、if(Build.VERSION.SDK_INT <24)

{

dropListPopupWindow.showAsDropDown(this,0,5);

}else{// 适配 android 7.0int[] location =newint[2];

getLocationOnScreen(location);intx = location[0];inty = location[1];

Log.e(getClass().getSimpleName(),"x : "+ x +", y : "+ y);

dropListPopupWindow.showAtLocation(this, Gravity.NO_GRAVITY,0, y + getHeight() +5);

}

二、大家要求重写popWindows的showAsDropDown方法:

@OverridepublicvoidshowAsDropDown(View anchor) {if(Build.VERSION.SDK_INT >=24) {            Rect rect =newRect();            anchor.getGlobalVisibleRect(rect);inth = anchor.getResources().getDisplayMetrics().heightPixels - rect.bottom;            setHeight(h);        }super.showAsDropDown(anchor);    }

自己提议第三个比较好

本文由王中王开奖结果发布于网络编程,转载请注明出处:遇到的坑,版本兼容问题集锦

关键词:

上一篇:Glide下载上传进程监听
下一篇:没有了