我发现ProgressDialog现在已经被弃用了。除了ProgressBar,还有什么替代工具可以代替它。 我使用的是android studio 2.3.3版。

ProgressDialog progressDialog=new ProgressDialog(this);
progressDialog.show();

当前回答

你可以使用AlertDialog作为ProgressDialog,参考下面的ProgressDialog代码。无论何时显示进度对话框,都需要调用此函数。

代码:

    public void setProgressDialog() {

    int llPadding = 30;
    LinearLayout ll = new LinearLayout(this);
    ll.setOrientation(LinearLayout.HORIZONTAL);
    ll.setPadding(llPadding, llPadding, llPadding, llPadding);
    ll.setGravity(Gravity.CENTER);
    LinearLayout.LayoutParams llParam = new LinearLayout.LayoutParams(
            LinearLayout.LayoutParams.WRAP_CONTENT,
            LinearLayout.LayoutParams.WRAP_CONTENT);
    llParam.gravity = Gravity.CENTER;
    ll.setLayoutParams(llParam);

    ProgressBar progressBar = new ProgressBar(this);
    progressBar.setIndeterminate(true);
    progressBar.setPadding(0, 0, llPadding, 0);
    progressBar.setLayoutParams(llParam);

    llParam = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
            ViewGroup.LayoutParams.WRAP_CONTENT);
    llParam.gravity = Gravity.CENTER;
    TextView tvText = new TextView(this);
    tvText.setText("Loading ...");
    tvText.setTextColor(Color.parseColor("#000000"));
    tvText.setTextSize(20);
    tvText.setLayoutParams(llParam);

    ll.addView(progressBar);
    ll.addView(tvText);

    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setCancelable(true);
    builder.setView(ll);

    AlertDialog dialog = builder.create();
    dialog.show();
    Window window = dialog.getWindow();
    if (window != null) {
        WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams();
        layoutParams.copyFrom(dialog.getWindow().getAttributes());
        layoutParams.width = LinearLayout.LayoutParams.WRAP_CONTENT;
        layoutParams.height = LinearLayout.LayoutParams.WRAP_CONTENT;
        dialog.getWindow().setAttributes(layoutParams);
    }
}

输出:

其他回答

正如文档页中提到的,替代方案是ProgressBar。ProgressDialog的外观可以通过在AlertDialog中放置ProgressBar来复制。

你仍然可以使用它,但Android不希望你使用它,这就是它被弃用的原因。所以你应该考虑用另一种方式解决你的问题,比如在你的布局中嵌入一个ProgressBar。

是的,ProgressDialog已弃用,但Dialog没有。

您可以将自己的XML文件(包含进度条和加载文本)扩展到对话框对象中,然后使用show()和dismiss()函数显示或隐藏它。 下面是一个例子(Kotlin):

ProgressDialog类:

class ProgressDialog {
companion object {
    fun progressDialog(context: Context): Dialog{
        val dialog = Dialog(context)
        val inflate = LayoutInflater.from(context).inflate(R.layout.progress_dialog, null)
        dialog.setContentView(inflate)
        dialog.setCancelable(false)
        dialog.window!!.setBackgroundDrawable(
                ColorDrawable(Color.TRANSPARENT))
        return 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:background="#fff"
android:padding="13dp"
android:layout_height="wrap_content">
<ProgressBar
    android:id="@+id/progressBar"
    style="?android:attr/progressBarStyle"
    android:layout_width="100dp"
    android:layout_margin="7dp"
    android:layout_height="100dp"/>
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerVertical="true"
    android:layout_margin="7dp"
    android:layout_toEndOf="@+id/progressBar"
    android:text="Loading..." />
</RelativeLayout>

在你的代码中: 只需执行var dialog = progressdialog。progressdialog (context)

显示方法:dialog.show()

隐藏方法:dialog.dismiss()

Kotlin和androidx DialogFragment的另一个解决方案

通过使用DialogFragment,你不必每次都在需要显示对话框的每个屏幕上调整窗口的大小,我认为这是一个更干净的解决方案

创建对话框类


class BaseProgressDialog : DialogFragment() {

    companion object {
        const val DIALOG_TAG = "BaseProgressDialogFragment"
    }

    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        return activity?.let {
            val builder = AlertDialog.Builder(it, R.style.MainAlertDialog)
            val inflater = requireActivity().layoutInflater
            val dialogView = inflater.inflate(R.layout.progress_dialog, null)

            builder.setView(dialogView)
            builder.create()
        } ?: throw IllegalStateException("Activity cannot be null")
    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        dialog?.setCanceledOnTouchOutside(false)
        dialog?.setCancelable(false)
        return super.onCreateView(inflater, container, savedInstanceState)
    }

    override fun onResume() {
        super.onResume()
        val size = resources.getDimensionPixelSize(R.dimen.size_80dp)
        dialog?.window?.setLayout(size, size)
    }
}

在styles.xml中创建样式

<style name="MainAlertDialog" parent="Theme.AppCompat.Light.Dialog.Alert">
    <item name="android:windowBackground">@drawable/bg_alert_dialog</item>
</style>

为对话框bg_alert_dialog.xml创建背景

<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="?attr/colorSecondary"/>
    <corners android:radius="@dimen/radius_10dp" />
</shape>

为对话框progress_dialog.xml创建布局

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/bg_alert_dialog"
    android:gravity="center"
    android:orientation="vertical"
    android:padding="@dimen/padding_16dp">

    <com.google.android.material.progressindicator.CircularProgressIndicator
        android:layout_width="@dimen/size_40dp"
        android:layout_height="@dimen/size_40dp"
        android:indeterminate="true"
        app:indicatorColor="?attr/colorPrimary" />

</LinearLayout>

调用片段中的对话框

private var progressDialog = BaseProgressDialog()
activity?.let {
   progressDialog.show(it.supportFragmentManager, BaseProgressDialog.DIALOG_TAG)
}

这是它看起来的样子:

也许这个指南可以帮助你。

通常我更喜欢用指示器制作自定义alertdialog。它解决了诸如自定义App视图等问题。

你可以使用AlertDialog作为ProgressDialog,参考下面的ProgressDialog代码。无论何时显示进度对话框,都需要调用此函数。

代码:

    public void setProgressDialog() {

    int llPadding = 30;
    LinearLayout ll = new LinearLayout(this);
    ll.setOrientation(LinearLayout.HORIZONTAL);
    ll.setPadding(llPadding, llPadding, llPadding, llPadding);
    ll.setGravity(Gravity.CENTER);
    LinearLayout.LayoutParams llParam = new LinearLayout.LayoutParams(
            LinearLayout.LayoutParams.WRAP_CONTENT,
            LinearLayout.LayoutParams.WRAP_CONTENT);
    llParam.gravity = Gravity.CENTER;
    ll.setLayoutParams(llParam);

    ProgressBar progressBar = new ProgressBar(this);
    progressBar.setIndeterminate(true);
    progressBar.setPadding(0, 0, llPadding, 0);
    progressBar.setLayoutParams(llParam);

    llParam = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
            ViewGroup.LayoutParams.WRAP_CONTENT);
    llParam.gravity = Gravity.CENTER;
    TextView tvText = new TextView(this);
    tvText.setText("Loading ...");
    tvText.setTextColor(Color.parseColor("#000000"));
    tvText.setTextSize(20);
    tvText.setLayoutParams(llParam);

    ll.addView(progressBar);
    ll.addView(tvText);

    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setCancelable(true);
    builder.setView(ll);

    AlertDialog dialog = builder.create();
    dialog.show();
    Window window = dialog.getWindow();
    if (window != null) {
        WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams();
        layoutParams.copyFrom(dialog.getWindow().getAttributes());
        layoutParams.width = LinearLayout.LayoutParams.WRAP_CONTENT;
        layoutParams.height = LinearLayout.LayoutParams.WRAP_CONTENT;
        dialog.getWindow().setAttributes(layoutParams);
    }
}

输出: