阅读量:2
在Android中,要在AlertDialog中显示进度条,可以使用ProgressBar组件和AlertDialog.Builder
- 首先,在XML布局文件中创建一个自定义的对话框布局。例如,创建一个名为
dialog_progress.xml的文件,并添加以下内容:
"1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp">
<ProgressBar
android:id="@+id/progressBar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:indeterminate="true"
android:max="100"/>
LinearLayout>
- 接下来,在你的Activity或Fragment中创建并显示一个包含进度条的AlertDialog。
// 使用LayoutInflater实例化自定义布局
LayoutInflater inflater = getLayoutInflater();
View dialogView = inflater.inflate(R.layout.dialog_progress, null);
// 创建一个AlertDialog.Builder
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setView(dialogView);
// 设置标题和按钮
builder.setTitle("正在处理")
.setCancelable(false)
.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// 在这里处理取消操作
}
});
// 显示AlertDialog
AlertDialog alertDialog = builder.create();
alertDialog.show();
现在,当你运行应用程序时,将显示一个包含进度条的AlertDialog。你可以根据需要自定义布局和对话框的外观。请注意,此示例使用了一个不确定的进度条,但你也可以通过设置android:indeterminate属性为false并更新ProgressBar的进度值来使用确定的进度条。
以上就是关于“怎样在AlertDialog中显示进度条”的相关介绍,筋斗云是国内较早的云主机应用的服务商,拥有10余年行业经验,提供丰富的云服务器、租用服务器等相关产品服务。云服务器资源弹性伸缩,主机vCPU、内存性能强悍、超高I/O速度、故障秒级恢复;电子化备案,提交快速,专业团队7×24小时服务支持!
简单好用、高性价比云服务器租用链接:https://www.jindouyun.cn/product/cvm