android遮盖颜色可以自定义的dialog

/**
 * @author xuti on 2017/8/21.
 */

public abstract class BaseDialog extends Dialog {
    protected ConstraintLayout mRootView;
    protected View             mContentView;

    private boolean mTouchCancelable = true;

    public BaseDialog(@NonNull Context context) {
        super(context);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        init();
    }

    protected void init() {
        mRootView = (ConstraintLayout) LayoutInflater.from(getContext()).inflate(getContentLayoutRes(), new ConstraintLayout(getContext()), true);
        mContentView = mRootView.getChildAt(0);

        setContentView(mRootView);

        {
            final Window dialogWindow = getWindow();
            // 获取对话框当前的参数值
            if (dialogWindow != null) {
                {
                    WindowManager.LayoutParams layoutParams = dialogWindow.getAttributes();
                    layoutParams.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL;
                    layoutParams.width = WindowManager.LayoutParams.MATCH_PARENT;
                    layoutParams.height = WindowManager.LayoutParams.MATCH_PARENT;
                    dialogWindow.setAttributes(layoutParams);
                }
                {
                    dialogWindow.setBackgroundDrawableResource(android.R.color.transparent);
                }
                {
                    //核心代码 解决了无法去除遮罩问题
                    dialogWindow.setDimAmount(0f);
                }
            }
        }

        StatusBarUtil.fullScreen(getWindow());

        {
            mContentView.setClickable(true);
            mRootView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (mTouchCancelable) {
                        dismiss();
                    }
                }
            });
        }

        initDialog(mRootView);
    }


    /**
     * 可以看到这里定义了一个抽象方法,这个将交由子类去实现
     *
     * @return
     */
    public abstract int getContentLayoutRes();

    protected abstract void initDialog(View view);

    public void setBackgroundColor(int color) {
        mRootView.setBackgroundColor(color);
    }

    /**
     * 设置遮罩颜色
     *
     * @param resid
     */
    public void setForegroundResid(int resid) {
        if (getWindow() != null) {
            View decorView = getWindow().getDecorView();
            if (decorView instanceof FrameLayout) {
                ((FrameLayout) decorView).setForeground(ContextCompat.getDrawable(getContext(), resid));
            }
        }
    }

    public void setTouchCancelable(boolean touchCancelable) {
        mTouchCancelable = touchCancelable;
    }

    /*************点击空白地方,输入法隐藏******************/
    private InputMethodManager mInputMethodManager = (InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE);

    @Override
    public boolean onTouchEvent(@NonNull MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            if (getCurrentFocus() != null && getCurrentFocus().getWindowToken() != null) {
                mInputMethodManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
            }
        }
        return super.onTouchEvent(event);
    }

    /*************点击空白地方,输入法隐藏******************/
}

 

StatusBarUtil.fullScreen(getWindow());在另一篇沉浸式状态栏文章中

 

发布了61 篇原创文章 · 获赞 4 · 访问量 3万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览