在项目开发中使用了沉浸式布局,所以对于dialog也需要沉浸式布局,刚开始使用dialog发现无法呈现沉浸式布局,在使用了个Activity的设置也不行,后面查询了一些资料,发现在高版本中需要设置layoutInDisplayCutoutMode参数,再此记录一下,下面是封装的代码(PS:代码封装使用的viewbinding)
/**
* @author Created by PengGuiChu on 2021/11/23 16:33.
* @explain
*/
public abstract class BaseDialog<VB extends ViewBinding> extends Dialog {
protected VB viewBind;
public BaseDialog(@NonNull Context context) {
super(context);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
viewBind = onCreateViewBinding(getLayoutInflater());
setContentView(viewBind.getRoot());
Window window = getWindow();
window.getDecorView().setSystemUiVisibility(getSystemUiVisibility());
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.setStatusBarColor(Color.TRANSPARENT);
WindowManager.LayoutParams layoutParams = window.getAttributes();//获取dialog布局的参数
if (getBackgroundDrawable()!=null){
window.setBackgroundDrawable(getBackgroundDrawable());
}
layoutParams.width = WindowManager.LayoutParams.MATCH_PARENT;//全屏
layoutParams.height = WindowManager.LayoutParams.MATCH_PARENT;//全屏
//设置导航栏颜
window.setNavigationBarColor(Color.TRANSPARENT);
//内容扩展到导航栏
window.setType(WindowManager.LayoutParams.TYPE_APPLICATION_PANEL);
if (Build.VERSION.SDK_INT>=28){
layoutParams.layoutInDisplayCutoutMode=WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES;
}
window.setAttributes(layoutParams);
initData();
}
protected abstract VB onCreateViewBinding(@NonNull LayoutInflater layoutInflater);
protected abstract void initData();
//设置背景颜色
protected Drawable getBackgroundDrawable(){
return null;
}
protected int getSystemUiVisibility(){
return SystemUiVisibilityUtil.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN|SystemUiVisibilityUtil.SYSTEM_UI_FLAG_LAYOUT_STABLE;
};
}
其中SystemUiVisibilityUtil是项目封装的常量类,替换成View即可
|