Android開發:PopupWindow優化 CompatPopupWindow

CompatPopupWindow

github

  • 優化PopupWindow在屏幕右邊或下邊的時候顯示不全的問題.

  • 提供一套AnimationStyle.


    調用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;
    }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章