软件编程
位置:首页>> 软件编程>> Android编程>> 详解Android中PopupWindow在7.0后适配的解决

详解Android中PopupWindow在7.0后适配的解决

作者:smart_dev  发布时间:2022-12-07 11:16:59 

标签:Android7.0,PopupWindow,适配

本文介绍了详解Android中PopupWindow在7.0后适配的解决,分享给大家,具体如下:

这里主要记录一次踩坑的经历。

需求:如上图左侧效果,想在按钮的下方弹一个PopupWindow。嗯,很简单一个效果,然当适配7.0后发现这个PopupWindow显示异常,然后网上找到了下面这种方案。

详解Android中PopupWindow在7.0后适配的解决

7.0适配方案(但7.1又复现了)


// 将popupWindow显示在anchor下方
public void showAsDropDown(PopupWindow popupWindow, View anchor) {
 if (Build.VERSION.SDK_INT < 24) {
  popupWindow.showAsDropDown(anchor);
 } else {
  // 适配 android 7.0
  int[] location = new int[2];
  // 获取控件在屏幕的位置
  anchor.getLocationOnScreen(location);
  popupWindow.showAtLocation(anchor, Gravity.NO_GRAVITY, 0, location[1] + anchor.getHeight());
 }
}

然后我那个开心啊,然后我就告诉其他人popwindow 在7.0 (SDK=24)适配的问题,然后所有popwindow都这么更改了。

尴尬的是7.1 (SDK=25)上又复现了这个问题,显示异常。

最终解决方案


if (Build.VERSION.SDK_INT < 24) {
mPopupWindow = new FixedPopupWindow(popView, ViewGroup.LayoutParams.MATCH_PARENT,
    ViewGroup.LayoutParams.MATCH_PARENT);
} else {
int[] location = new int[2];
// 获取控件在屏幕的位置
anchor.getLocationOnScreen(location);
int screenHeight = getScreenHeightPixels(context);
mPopupWindow = new PopupWindow(popView, ViewGroup.LayoutParams.MATCH_PARENT,
    screenHeight - (location[1] + anchor.getHeight()));
}

在初始化的时候通过动态设置高度来完成显示效果。此时我们直接调用显示就行了。


mPopupWindow.showAsDropDown(anchor);

小思考

当项目中公用PopupWindow的时候,你一定想着封装一次,毕竟PopupWindow的初始化也是一个体力活。于是,可以将这种适配方案直接在showAsDropDown()方法中实现。


import android.graphics.Rect;
import android.os.Build;
import android.view.View;
import android.widget.PopupWindow;

/**
* Created by smart on 2018/5/15.
*/

public class FixedPopupWindow extends PopupWindow {

public FixedPopupWindow(View contentView, int width, int height){
 super(contentView, width, height);

}
.....

@Override
public void showAsDropDown(View anchor) {
 if (Build.VERSION.SDK_INT >= 24) {
  Rect rect = new Rect();
  anchor.getGlobalVisibleRect(rect);// 以屏幕 左上角 为参考系的
  int h = anchor.getResources().getDisplayMetrics().heightPixels - rect.bottom; //屏幕高度减去 anchor 的 bottom
  setHeight(h);// 重新设置PopupWindow高度
 }
 super.showAsDropDown(anchor);
}
...
}

与上面那种方案比较

  1. 两种不同的计算高度的方法

  2. 都是通过设置PopupWindow的高度实现

  3. 这种封装可以简化重用代码

来源:https://www.jianshu.com/p/df010d92f646

0
投稿

猜你喜欢

手机版 软件编程 asp之家 www.aspxhome.com