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

相关推荐
雨白20 小时前
优雅地处理协程:取消机制深度剖析
android·kotlin
leon_zeng021 小时前
更改 Android 应用 ID (ApplicationId) 后遭遇记
android·发布
2501_916007471 天前
iOS 混淆工具链实战,多工具组合完成 IPA 混淆与加固(iOS混淆|IPA加固|无源码混淆|App 防反编译)
android·ios·小程序·https·uni-app·iphone·webview
Jeled1 天前
Retrofit 与 OkHttp 全面解析与实战使用(含封装示例)
android·okhttp·android studio·retrofit
ii_best1 天前
IOS/ 安卓开发工具按键精灵Sys.GetAppList 函数使用指南:轻松获取设备已安装 APP 列表
android·开发语言·ios·编辑器
2501_915909061 天前
iOS 26 文件管理实战,多工具组合下的 App 数据访问与系统日志调试方案
android·ios·小程序·https·uni-app·iphone·webview
limingade1 天前
手机转SIP-手机做中继网关-落地线路对接软交换呼叫中心
android·智能手机·手机转sip·手机做sip中继网关·sip中继
RainbowC01 天前
GapBuffer高效标记管理算法
android·算法
程序员码歌1 天前
豆包Seedream4.0深度体验:p图美化与文生图创作
android·前端·后端
weixin_lynhgworld1 天前
海外盲盒APP开发:从“未知”到“精准”的用户体验革命
app·海外盲盒