全局居中dialog

复制代码
public class FullScreenDialog extends Dialog {

    public FullScreenDialog(Context context) {
        super(context);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        // 按空白处不能取消
        setCanceledOnTouchOutside(false);
    }

    @Override
    public void show() {
        final Window window = getWindow();
        if (window != null) {
            // 获取焦点,防止导航栏闪烁
            window.setFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
                    WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE);
        }
        super.show();
        hideNavigationBar(window);
        if (window == null) {
            return;
        }
        window.clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE);
        window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_NOTHING);
        window.setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT);
        window.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        // 设置全屏属性
        window.addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
        // 设置window背景透明
        window.setBackgroundDrawableResource(android.R.color.transparent);
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.alpha = 1.0f;
        lp.dimAmount = 0.0f; //dimAmount在0.0f和1.0f之间,0.0f完全不暗,1.0f全暗
        lp.format = PixelFormat.TRANSPARENT;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
            // 延伸显示区域到刘海
            lp.layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES;
        }
        window.setAttributes(lp);
        window.getDecorView().setPadding(0, 0, 0, 0);
    }

    /**
     * 隐藏导航栏
     *
     * @param window window
     */
    public void hideNavigationBar(final Window window) {
        if (window == null) {
            return;
        }
        final int uiOptions = View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
                // Set the content to appear under the system bars so that the
                // content doesn't resize when the system bars hide and show.
                | View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                // Hide the nav bar and status bar
                | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                | View.SYSTEM_UI_FLAG_FULLSCREEN;
        window.getDecorView().setSystemUiVisibility(uiOptions);
    }
}
相关推荐
唐老板11 分钟前
Spring AI 2.0 只支持 Boot 4
java
琳少_3 小时前
java程序设计笔记
java·开发语言·数据库·笔记
小荷才露尖尖角,早有蜻蜓立上头4 小时前
SpringBoot 定时任务
java·spring boot·后端
Helix2505 小时前
JDK高频报错速查手册:javac不是内部命令、内存溢出、版本不兼容等12类问题解决方法
java·开发语言·内存溢出·java异常处理·jdk版本兼容·jdk报错·javac报错
CoderYanger6 小时前
A.每日一题:2243. 计算字符串的数字和
java·开发语言·程序人生·leetcode·面试·蓝桥杯·学习方法
摇滚侠7 小时前
《On Java 中文版 基础卷》阅读笔记 接口 19
java·开发语言·笔记
吃我一窝jio8 小时前
Java多线程核心知识点梳理
java·开发语言·笔记
这个昵称不容易11 小时前
在线考试系统设计与实现:14 张表 158 个接口,组卷、答题、判分一条线走通
java·springboot·毕设
码云社区11 小时前
Spring Boot上门按摩服务系统源码:上门到店服务目录与门店协同方案
java·spring boot·后端·上门服务·上门按摩·上门小程序
30955666611 小时前
股票数据落库前的去重与主键设计:一次 SQLite 写入实录
java·开发语言·jvm·数据库·redis·python·sqlite