美文网首页
Android学习笔记1--全屏ProgressDialog

Android学习笔记1--全屏ProgressDialog

作者: kjiwu | 来源:发表于2017-06-22 23:07 被阅读938次

    需求

    ProgressDialog全屏显示,加载进度在屏幕正中间,有背景颜色;

    关键代码

        private void showFullScreenDialog() {
            Dialog dialog = new Dialog(this, R.style.dialog_style);
            //设置是否允许Dialog可以被点击取消,也会阻止Back键
            dialog.setCancelable(false);
            //获取Dialog窗体的根容器
            LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            ViewGroup root = (ViewGroup) dialog.getWindow().getDecorView().findViewById(android.R.id.content);
            //设置窗口大小为屏幕大小
            WindowManager wm = (WindowManager) getApplicationContext().getSystemService(Context.WINDOW_SERVICE);
            Point screenSize = new Point();
            wm.getDefaultDisplay().getSize(screenSize);
            root.setLayoutParams(new LinearLayout.LayoutParams(screenSize.x, screenSize.y));
            //获取自定义布局,并设置给Dialog
            View view = inflater.inflate(R.layout.dialog_layout, root, false);
            dialog.setContentView(view, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
            dialog.show();
        }
    

    R.style.dialog_style样式

        <style name="dialog_style" parent="Theme.AppCompat.Dialog">
            <!--设置背景颜色-->
            <item name="android:windowBackground">@android:color/transparent</item>
            <!--控制全屏-->
            <item name="android:windowNoTitle">true</item>
            <item name="android:windowFullscreen">true</item>
            <item name="android:windowIsTranslucent">true</item>
            <!--设置是否显示"遮罩"层-->
            <item name="android:backgroundDimEnabled">true</item>
        </style>
    

    相关文章

      网友评论

          本文标题:Android学习笔记1--全屏ProgressDialog

          本文链接:https://www.haomeiwen.com/subject/sqdqcxtx.html