需求
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>
网友评论