java - 如何让ProgressDialog背景覆盖整个屏幕?

标签 java android android-studio

我使用以下代码添加进度对话框:

progress_dialog.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center"
    android:orientation="vertical"
    android:windowIsFloating="false"
    android:background="@android:color/white">

    <ProgressBar
        android:id="@+id/progressBar1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:layout_centerInParent="true" />
</RelativeLayout>

MainActivity.java

public void onProgressChanged(WebView view, int progress) {
                if (mProgress == null) {

                   
                    mProgress = new ProgressDialog(MainActivity.this);

                    // Setting progress dialog color as transparent
                    mProgress.getWindow().setBackgroundDrawable(new ColorDrawable(Color.WHITE));

                    mProgress.show();
                    mProgress.setContentView(R.layout.progress_dialog);
                    mProgress.setCancelable(false);
                    mProgress.setCanceledOnTouchOutside(false);
                    // Added to prevent touch interaction while loading
                    //   getWindow().setFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE,
                    //         WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
                }
                // Setting text on progress bar
                // mProgress.setMessage("Loading " + String.valueOf(progress) + "%");
                if (progress == 100) {
                    mProgress.dismiss();
                    mProgress = null;
                    // Resume touch interaction once loaded
                    // getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);

                }
            }

这显示了如下所示的进度对话框:

This is how the progressdialog appears

我希望进度对话框覆盖整个屏幕,如下所示:

enter image description here

如何使用 ProgressDialog 实现此目的?

最佳答案

试试这个:

首先,您必须在样式文件中创建主题,如下所示:

  <style name="CustomDialogTheme" parent="Theme.AppCompat.Light.DialogWhenLarge">
            <item name="android:windowIsFloating">false</item>
            <item name = "android:windowBackground" >@android:color/white</item >
        </style>

在上面的样式中,我放置了白色昏暗背景

并在 java 代码中的对话框中应用此样式:

@SuppressLint("InflateParams")
private void showDialog() {
    final Dialog dialog = new Dialog(this, R.style.CustomDialogTheme);//apply style here
    View view = LayoutInflater.from(this).inflate(R.layout.progress, null);// inflat the custom view
    WindowManager.LayoutParams params = dialog.getWindow().getAttributes();
    params.width = WindowManager.LayoutParams.MATCH_PARENT; // set as full width
    params.height = WindowManager.LayoutParams.MATCH_PARENT;// set as full heiggt
    dialog.setContentView(view); //add the custom view
    dialog.getWindow().setGravity(Gravity.CENTER); // set center gravity
    dialog.show();
}

最后这是自定义布局:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <ProgressBar
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center" />
</FrameLayout>

关于java - 如何让ProgressDialog背景覆盖整个屏幕?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66860509/

相关文章:

Java GUI 程序打开一个新的 JPanel,而不是替换当前面板

java - 将可执行文件 .jar 嵌入 jframe 中

android - 添加自定义日历事件 – Phonegap Android 插件

java - 需要添加 float 操作按钮错误android中fragment页面中的多个根标签

java - 当我单击按钮时导航屏幕打开,但当我再次单击它时它不会再次关闭

Java 互斥

java - 为字符串创建随机数

android - 对在应用程序中使用 Fragments 感到困惑

android - Weui 超链接在 Android 上不起作用

java - 使用 Android Studio 3.5 的 Kotlin 中的资源文件和 const val 之间哪种方式更好?