public abstract class BaseDialog extends Dialog {
protected ConstraintLayout mRootView;
protected View mContentView;
private boolean mMyCancelable = true;
private View.OnClickListener mMyOnClickOutsideListener;
public BaseDialog(@NonNull Context context) {
super(context, R.style.BaseDialog);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
init();
}
protected void init() {
mRootView = (ConstraintLayout) LayoutInflater.from(getContext()).inflate(getContentLayoutRes(), new ConstraintLayout(getContext()), true);
mRootView.setFitsSystemWindows(true);
mContentView = mRootView.getChildAt(0);
setContentView(mRootView);
{
setMaskColor(R.color.colorBlack9913131A);
}
{
//设置空白区域的颜色,显示内容部分不会被遮住
setMaskColor(R.color.colorBlack9913131A);
}
{
ButterKnife.bind(this, 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);
}
}
}
ImmersiveStatusBarUtils.setImmersiveStatus(getWindow(), false);
{
mContentView.setClickable(true);
mRootView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (mMyCancelable) {
dismiss();
}
if (mMyOnClickOutsideListener != null) {
mMyOnClickOutsideListener.onClick(view);
}
}
});
}
//继承类不用调用此方法
setCancelable(false);
initDialog(mRootView);
SoftKeyBoardListener softKeyBoardListener = new SoftKeyBoardListener(getWindow());
//软键盘状态监听
softKeyBoardListener.setListener(new SoftKeyBoardListener.OnSoftKeyBoardChangeListener() {
@Override
public void keyBoardShow(int height) {
//软键盘已经显示,做逻辑
if ((ScreenUtils.getScreenHeight() - mContentView.getBottom()) >= height) {
return;
}
ConstraintLayout.LayoutParams layoutParams = (ConstraintLayout.LayoutParams) mContentView.getLayoutParams();
layoutParams.topToTop = ConstraintLayout.LayoutParams.UNSET;
layoutParams.bottomToBottom = ConstraintLayout.LayoutParams.PARENT_ID;
layoutParams.startToStart = ConstraintLayout.LayoutParams.PARENT_ID;
layoutParams.endToEnd = ConstraintLayout.LayoutParams.PARENT_ID;
layoutParams.bottomMargin = height;
mContentView.setLayoutParams(layoutParams);
}
@Override
public void keyBoardHide(int height) {
//软键盘已经隐藏,做逻辑
ConstraintLayout.LayoutParams layoutParams = (ConstraintLayout.LayoutParams) mContentView.getLayoutParams();
layoutParams.topToTop = ConstraintLayout.LayoutParams.PARENT_ID;
layoutParams.bottomToBottom = ConstraintLayout.LayoutParams.PARENT_ID;
layoutParams.startToStart = ConstraintLayout.LayoutParams.PARENT_ID;
layoutParams.endToEnd = ConstraintLayout.LayoutParams.PARENT_ID;
layoutParams.bottomMargin = 0;
mContentView.setLayoutParams(layoutParams);
}
});
}
/**
* 可以看到这里定义了一个抽象方法,这个将交由子类去实现
*
* @return
*/
public abstract int getContentLayoutRes();
protected abstract void initDialog(View view);
/**
* 设置遮罩颜色
*
* @param colorRes
*/
public void setMaskColor(int colorRes) {
mRootView.setBackgroundResource(colorRes);
}
public void setMyOnClickOutsideListener(View.OnClickListener myOnClickOutsideListener) {
mMyOnClickOutsideListener = myOnClickOutsideListener;
}
/*************点击空白地方,输入法隐藏******************/
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);
}
/*************点击空白地方,输入法隐藏******************/
public void show(boolean cancelable) {
mMyCancelable = cancelable;
show();
}
}
<style name="BaseDialog" parent="android:style/Theme.Dialog">
<!-- 第一个是dialog的背景色,当然你可以设置图片,不拦大家 -->
<item name="android:background">@android:color/transparent</item>
<!-- 第二个就是弹出dialog后,下面的activity层的颜色啦-->
<item name="android:windowBackground">@android:color/transparent</item>
<!--这里设置为没有标题栏,如果这里不设置的话,你会发现无论布局怎么改都会出现多一栏白色 -->
<item name="android:windowNoTitle">true</item>
<item name="android:windowFrame">@null</item><!--边框-->
</style>
public class ImmersiveStatusBarUtils {
/**
* 沉浸式状态栏,并设置文字颜色
*
* @param window
*/
public static void setImmersiveStatus(Window window, boolean textColorIsBlack) {
if (window == null) {
return;
}
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
return;
}
int option;
if (textColorIsBlack) {
option = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
} else {
option = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
}
window.getDecorView().setSystemUiVisibility(option);
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.setStatusBarColor(Color.TRANSPARENT);
fitsNotchScreen(window);
}
/**
* 适配刘海屏
* Fits notch screen.
*/
private static void fitsNotchScreen(Window window) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
WindowManager.LayoutParams lp = window.getAttributes();
lp.layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES;
window.setAttributes(lp);
}
}
}