Android开发:PopupWindow优化

CompatPopupWindow

github

  • 优化PopupWindow在屏幕右边或下边的时候显示不全的问题.

  • 提供一套AnimationStyle.


    Android开发:PopupWindow优化_第1张图片
    popup.gif

    调用showAsDropDown时,如果anchor太靠下,可能空间不够,造成PopupWindow显示不全,所以要判断进行y方向偏移。靠右的画gravity可以用Gravity.END|Gravity.Top解决。

核心代码

@Override
    public void showAsDropDown(View anchor, int xoff, int yoff, int gravity) {
        int adjustYoff = adjustYoff(anchor, yoff);
        super.showAsDropDown(anchor, xoff, adjustYoff, gravity);
    }

    private int adjustYoff(View anchor, int yoff) {
        if (anchor == null) {
            return 0;
        }
        anchor.getLocationInWindow(location);
        int availableHeight = UtilsSize.getScreenHeight(anchor.getContext()) - location[1] - anchor.getHeight();
        int minWidowHeight = getMinWidowHeight();
        if (availableHeight < minWidowHeight && minWidowHeight != -1) {
            yoff -= minWidowHeight;
            if (useAnimStyle) {
                setAnimationStyle(R.style.AnimStyle_Bottom);
            }
        } else {
            if (useAnimStyle) {
                setAnimationStyle(R.style.AnimStyle);
            }
        }
        return yoff;
    }

你可能感兴趣的:(Android开发:PopupWindow优化)