Android现代进度条替代方案

在 Android 开发中,ProgressDialog 自 API 26(Android 8.0)起已被标记为过时 (deprecated),官方推荐使用更灵活的组件替代,例如 ProgressBarAlertDialog 结合进度指示器,或者 Material Design 组件库中的 ProgressIndicator。以下是几种常用且符合现代规范的替代方案:


1. 使用 ProgressBar 组件

直接在布局中添加 ProgressBar,并通过代码控制其显示/隐藏。这是最简单、轻量的方式。

XML 布局示例:

xml 复制代码
<ProgressBar
    android:id="@+id/progressBar"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:indeterminate="true"  <!-- 不确定进度模式(循环动画) -->
    android:visibility="gone" />

代码控制:

kotlin 复制代码
val progressBar = findViewById<ProgressBar>(R.id.progressBar)
progressBar.visibility = View.VISIBLE // 显示
progressBar.visibility = View.GONE   // 隐藏

2. Material Design 的 CircularProgressIndicator

使用 Material Components 库中的组件,提供更现代的 UI 风格。

步骤 1:添加依赖

gradle 复制代码
implementation 'com.google.android.material:material:1.9.0'

XML 布局示例:

xml 复制代码
<com.google.android.material.progressindicator.CircularProgressIndicator
    android:id="@+id/progressIndicator"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:indicatorColor="@color/primary"
    app:trackColor="@color/background" />

代码控制:

kotlin 复制代码
val progress = findViewById<CircularProgressIndicator>(R.id.progressIndicator)
progress.visibility = View.VISIBLE // 显示
progress.visibility = View.INVISIBLE // 隐藏

3. 自定义 AlertDialog 实现加载对话框

通过 AlertDialog 自定义布局,模拟类似 ProgressDialog 的效果,同时避免过时 API。

示例代码(Kotlin):

kotlin 复制代码
fun showLoadingDialog(context: Context): AlertDialog {
    val progressBar = ProgressBar(context).apply {
        isIndeterminate = true
        setPadding(32, 32, 32, 32)
    }

    val dialog = AlertDialog.Builder(context)
        .setView(progressBar)
        .setMessage("加载中...")
        .setCancelable(false)
        .create()

    dialog.show()
    return dialog
}

// 使用
val loadingDialog = showLoadingDialog(this)
// 关闭时调用 loadingDialog.dismiss()

4. Material Design 的 LinearProgressIndicator(水平进度条)

适用于显示确定进度的场景。

XML 布局:

xml 复制代码
<com.google.android.material.progressindicator.LinearProgressIndicator
    android:id="@+id/linearProgress"
    android:layout_width="match_parent"
    android:layout_height="4dp"
    app:indicatorColor="@color/primary"
    app:trackColor="@color/background" />

代码控制进度:

kotlin 复制代码
val linearProgress = findViewById<LinearProgressIndicator>(R.id.linearProgress)
linearProgress.progress = 50 // 设置进度百分比(0-100)

关键注意事项:

  1. 避免阻塞主线程 :长时间任务应使用异步机制(如 CoroutineRxJavaAsyncTask)。
  2. 生命周期管理 :在 Activity/Fragment 销毁时关闭对话框,防止内存泄漏。
  3. Material Design 一致性:优先使用 Material 组件库以保持 UI 统一。

通过这些方法,你可以完全替代过时的 ProgressDialog,同时提升用户体验和代码健壮性。

相关推荐
dualven_in_csdn9 小时前
一键起飞调用示例
android·java·javascript
故渊at9 小时前
第十板块:Android 系统稳定性与调试 | 第二十五篇:Watchdog 与 ANR 的系统级监控
android·watchdog·系统稳定性·anr·超时监控
故渊at10 小时前
第十板块:Android 系统稳定性与调试 | 第二十六篇:Systrace 与 Perfetto 的系统级性能分析
android·perfetto·性能分析·systrace·系统稳定性
吕工-老船长199810 小时前
20260610----S905Y5(Android14)-----连接网络自动更新时间,时间设置为24小时
android
杉氧12 小时前
Kotlin 协程深度解析④:架构实战——在 MVVM/MVI 中的进阶应用
android·kotlin
Ab_stupid12 小时前
CTF-Android培训笔记
android·笔记
Ycocol12 小时前
AS同一个目录下的类导入导入其他类爆红无法跳转但是可以编译
android·ide·android studio
zandy101112 小时前
SaaS 多租户架构设计实践:衡石 BI 如何实现数据隔离与资源调度
app·多租户·isv
Meteors.13 小时前
安卓字节码插桩与埋点
android