PopupWindow總結和通用PopupWindow

PopupWindowide

  • 注意點:
  1. 設置PopupWindow的寬高,容易忘記,沒有設置寬高在某些狀況下會不展現。ui

  2. 基礎設置少不了this

pop.setBackgroundDrawable(new BitmapDrawable());
pop.setFoucusable(true);
  1. 擼一個通用的PopupWindow
public  class BasicPopupWindow extends PopupWindow {

    private Context mContext;
    private int layoutId;
    private ViewController controller;
    private int animStyleId;
    private boolean isOutsideTouchable;

    private BasicPopupWindow(Context context){
        this.mContext = context;
        initDefault();
    }

    private void initDefault()
    {
        setBackgroundDrawable(new ColorDrawable());
        setFocusable(true);
        setTouchable(true);
        setOutsideTouchable(true);
        setClippingEnabled(true); //是否容許超出屏幕範圍
    }

    private void init() {
        //寬高設置放在builder中
        View contentView = LayoutInflater.from(mContext).inflate(layoutId,null);
        controller.handleView(contentView);
        setContentView(contentView);
    }

    public void showAtBottom(View anchorView) {
        if(null == anchorView){
            return;
        }
        if(!isShowing()){
            backgroundDimAmount(0.5f, (Activity) mContext);
            showAtLocation(anchorView, Gravity.BOTTOM,0,0);
        }
    }

    public void showAtCenter(View anchorView)
    {
        if(null == anchorView){
            return;
        }
        if(!isShowing()){
            backgroundDimAmount(0.5f, (Activity) mContext);
            showAtLocation(anchorView,Gravity.CENTER,0,0);
        }
    }

    //1 全黑   0 正常  設置昏暗度
    public void backgroundDimAmount(float dimAmount, Activity activity)
    {
        if(null == activity){
            return;
        }
        Window window = activity.getWindow();
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.dimAmount = dimAmount;
        window.setAttributes(lp);
    }

    //設置透明度
    private void backgroundAlpha(float alpha, Activity activity)
    {
        if(null == activity){
            return;
        }
        Window window = activity.getWindow();
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.alpha = alpha;
        window.setAttributes(lp);
    }

    public void setViewController(ViewController viewController)
    {
        this.controller = viewController;
    }

    public void setLayoutId(int layoutId)
    {
        this.layoutId = layoutId;
    }


    public interface ViewController {
        void handleView(View view);
    }

    public static class Builder{

        private int layoutId;
        private Context mContext;
        private ViewController viewController;
        private int width;
        private int height;
        private int animStyleId;
        private boolean isOutsideTouchable = true;

        //必須
        public void setLayoutId(@LayoutRes int layoutId) {
            this.layoutId = layoutId;
        }

        //必須
        public void setContext(Context context){
            this.mContext = context;
        }

        //必須
        public void setViewController(ViewController viewController) {
            this.viewController = viewController;
        }

        public void setWidth(int width) {
            this.width = width;
        }

        public void setOutsideTouchable(boolean outsideTouchable) {
            this.isOutsideTouchable = outsideTouchable;
        }

        public void setHeight(int height){
            this.height = height;
        }

        public void setAnimStyle(@AnimRes int animStyleId){
            this.animStyleId = animStyleId;
        }

        public BasicPopupWindow build(){
            BasicPopupWindow popWindow = new BasicPopupWindow(mContext);
            popWindow.setViewController(viewController);
            popWindow.setLayoutId(layoutId);
            if(0 == width){
                width = ViewGroup.LayoutParams.MATCH_PARENT;
            }
            if(0 == height){
                height = ViewGroup.LayoutParams.WRAP_CONTENT;
            }
            popWindow.setWidth(width);
            popWindow.setHeight(height);
            if(0 != animStyleId){
                popWindow.setAnimationStyle(animStyleId);
            }
            popWindow.setOutsideTouchable(isOutsideTouchable);
            popWindow.init();
            return popWindow;
        }
    }
}
相關文章
相關標籤/搜索