Android dialog沉浸式状态栏

在项目开发中使用了沉浸式布局,所以对于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即可

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值