클릭하여 사라짐 Toast - 사용자 정의View dialog

2772 단어
투명한 배경을 누르면 사용자 정의view 사용자 정의 위치가 사라집니다.
public static class CustomViewDialog extends Dialog {
        private int gravity, xOffset, yOffset;

        public CustomViewDialog(View contentView, int duration) {
            super(HSApplication.getContext(), R.style.CustomToastDialog);
            init(contentView, duration, Gravity.CENTER, 0, 0);
        }


        public CustomViewDialog(View contentView, int duration, int gravity, int xOffset, int yOffset) {
            super(HSApplication.getContext(), R.style.CustomToastDialog);
            init(contentView, duration, gravity, xOffset, yOffset);
        }

        private void init(View contentView, int duration, int gravity, int xOffSet, int yOffSet) {
            setContentView(contentView);
            setTitle(null);
            contentView.postDelayed(new Runnable() {
                @Override
                public void run() {
                    CustomViewDialog.this.cancel();
                }
            }, duration);
            setCanceledOnTouchOutside(true);

            this.gravity = gravity;
            this.xOffset = xOffSet;
            this.yOffset = yOffSet;
        }


        @Override
        public void show() {
            Window window = getWindow();
            if (window != null) {
                window.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
                WindowManager.LayoutParams params = window.getAttributes();
                params.y = yOffset;
                params.x = xOffset;
                window.setGravity(gravity);
                window.setAttributes(params);
                window.setType(WindowManager.LayoutParams.TYPE_TOAST);
            }
            super.show();
        }

        @Override
        public boolean dispatchTouchEvent(MotionEvent ev) {
            return onTouchEvent(ev);
        }

        @Override
        public boolean onTouchEvent(MotionEvent event) {
            cancel();
            return false;
        }

    }

스타일 파일
    

좋은 웹페이지 즐겨찾기