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

时间:2021-03-29 19:32:38

标签: 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 完成此操作?

3 个答案:

答案 0 :(得分:1)

在进度条中将宽度和高度设置为 match_parent。

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

答案 1 :(得分:1)

试试这个:

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

  <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>

答案 2 :(得分:1)

这很好,但我建议您在将来需要时将其用于不同的类,只需调用类名并实现它

public class ShowDialogClass {

    public static ProgressDialog showDialog(Context context) {
   
        final Dialog dialog = new Dialog(context, R.style.CustomDialogTheme);//apply style here
        View view = LayoutInflater.from(context).inflate(R.layout.progress, null);// inflate 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 height
        dialog.setContentView(view); //add the custom view
        dialog.getWindow().setGravity(Gravity.CENTER); // set center gravity
        return dialog;
    }

}

以及如何使用它:

Dialog dialog= ShowDialogClass.showDialog(this)
dialog.show()
dialog.dismiss()
相关问题