在控件的上下左右彈出popwidown


很簡單的一個自定義的popwindown的基本類,主要功能是在任意佈局的上下左右彈出pop對話框,如果不夠用,自己修改代碼在任意位置彈出pop窗口,也很簡單。



<span style="font-size:18px;">public class BaseCustomPop extends PopupWindow {
    private View rootView;//pop的佈局
    private int[] location = new int[2];
    private int margin = 10;//pop與控件的距離間隔

    public BaseCustomPop(View view) {
        this(view, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    }

    public BaseCustomPop(View view, int width, int height) {
        this(view, width, height, 0);
    }

    /**
     * @param layoutView
     * @param width
     * @param height
     * @param animationStyle
     */
    public BaseCustomPop(View layoutView, int width, int height, int animationStyle) {
//        setAnimationStyle(R.style.pop_anim);
        this.setWidth(width);
        this.setHeight(height);
        this.rootView = layoutView;
        this.setAnimationStyle(animationStyle);
        this.setFocusable(true);
        this.setOutsideTouchable(true);
        this.setBackgroundDrawable(new BitmapDrawable());
        this.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        this.setContentView(layoutView);
    }

    public void show(View anchor) {
        show(anchor, POPGriVity.TOP);
    }

    public enum POPGriVity {
        TOP, BOTTOM, LEFT, RIGHT
    }


    public void setMargin(int margin) {
        this.margin = margin;
    }

    public void show(View anchor, POPGriVity grivity) {
        if (rootView == null)
            throw new IllegalStateException("setContentView was not called with a view to display.");
        if (anchor == null)
            throw new NullPointerException("anchor is null");

        int xPos, yPos;

        anchor.getLocationOnScreen(location);
        final Rect anchorRect = new Rect(location[0], location[1], location[0] + anchor.getWidth(),
                location[1] + anchor.getHeight());
        rootView.measure(0, 0);
        final int rootHeight = rootView.getMeasuredHeight();
        final int rootWidth = rootView.getMeasuredWidth();
        final int anchorCenterPosX = anchorRect.centerX();
        //默認顯示在上面
        xPos = anchorCenterPosX - rootWidth / 2;
        yPos = anchorRect.top - rootHeight - margin;
        if (grivity == POPGriVity.TOP) {
            xPos = anchorCenterPosX - rootWidth / 2;
            yPos = anchorRect.top - rootHeight - margin;
        } else if (grivity == POPGriVity.BOTTOM) {
            xPos = anchorCenterPosX - rootWidth / 2;
            yPos = anchorRect.bottom + margin;
        } else if (grivity == POPGriVity.LEFT) {
            yPos = anchorRect.centerY()- rootHeight / 2;
            xPos = anchorRect.left - rootWidth - margin;
        } else if (grivity == POPGriVity.RIGHT) {
            yPos = anchorRect.centerY()- rootHeight / 2;
            xPos = anchorRect.right + margin;
        }
        showAtLocation(anchor, Gravity.NO_GRAVITY, xPos, yPos);
    }
}</span>


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章