ProgressDialog的备选项



在API级别26中,ProgressDialog已被弃用。在我的应用程序中,我想显示我的下载任务的进度与标题和取消选项。另一种方法是什么?

为什么不添加ProgressView到您的Dialog视图,而无需为Dialog

创建自定义布局
LinearLayout linearLayout = new LinearLayout(this);
linearLayout.setGravity(Gravity.CENTER);
ProgressBar progressBar = new ProgressBar(this);
LinearLayout.LayoutParams progressParam = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
progressParam.setMargins(0, 40, 0, 40);// set margin to progressBar
progressBar.setLayoutParams(progressParam);
linearLayout.addView(progressBar);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Loading ")
.setMessage("Please waite until the map loaded")
.setView(linearLayout).setCancelable(false)
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();     
}
});

builder.create().show();

最新更新