在控件的上下左右弹出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>


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值