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,同时提升用户体验和代码健壮性。

相关推荐
四维碎片4 小时前
【Qt】配置安卓开发环境
android·开发语言·qt
百***99244 小时前
MySql的慢查询(慢日志)
android·mysql·adb
安卓兼职framework应用工程师4 小时前
android 15.0 Launcher3长按拖拽时,获取当前是哪一屏,获取当前多少个应用图标
android·拖拽·workspace·长按拖拽
雨白4 小时前
Jetpack Compose Navigation3:返回栈管理、大屏适配与自定义策略
android
CIb0la8 小时前
安卓16系统升级后(Google pixel 8/8pro 9/9pro xl 10/10pro xl)救砖及Root方法
android·运维·生活
Ya-Jun8 小时前
项目实战Now in Android:项目模块说明
android·架构·kotlin
@Aurora.9 小时前
【MySQL】基础
android
ooooooctober9 小时前
PHP代码审计框架性思维的建立
android·开发语言·php
q***829110 小时前
图文详述:MySQL的下载、安装、配置、使用
android·mysql·adb
沐怡旸11 小时前
【底层机制】Ashmem匿名共享内存:原理与应用深度解析
android·面试