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

相关推荐
&岁月不待人&1 天前
⏺ Android 录屏缩放异常排查:Pixel 3 XL 上的完美风暴
android
a3158238061 天前
Android 大图显示策略优化显示(一)
android·算法·图片加载·大图片
tangweiguo030519871 天前
从零开始:在 Windows 上使用命令行编译 Android .so 动态库(NDK + CMake + Ninja)
android
阿波罗尼亚1 天前
Tcp SSE Utils
android·java·tcp/ip
知行合一。。。1 天前
Python--03--函数入门
android·数据库·python
大、男人1 天前
python之contextmanager
android·python·adb
不法1 天前
java查看安卓证书信息
android
儿歌八万首1 天前
Jetpack Compose 动画实战:让你的 UI 动起来
android·kotlin·动画·compose
千里马学框架1 天前
如何改进车载三分屏SplitScreen启动交互方式?
android·智能手机·分屏·aaos·安卓framework开发·车载开发·3分屏
iOS阿玮1 天前
“死了么”App荣登付费榜第一名!
uni-app·app·apple