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

相关推荐
百锦再2 小时前
Android Studio开发中Application和Activity生命周期详解
android·java·ide·app·gradle·android studio·studio
万户猴3 小时前
【Android蓝牙开发实战-11】蓝牙BLE多连接机制全解析1
android·蓝牙
RichardLai883 小时前
[Flutter 基础] - Flutter基础组件 - Icon
android·flutter
前行的小黑炭3 小时前
Android LiveData源码分析:为什么他刷新数据比Handler好,能更节省资源,解决内存泄漏的隐患;
android·kotlin·android jetpack
清霜之辰3 小时前
安卓 Compose 相对传统 View 的优势
android·内存·性能·compose
_祝你今天愉快3 小时前
再看!NDK交叉编译动态库并在Android中调用
android
一杯凉白开3 小时前
Android View 事件的分发机制 四句口诀 先问拦截再派送,子不处理父兜底, 一旦消费无后续, 滑动冲突靠逻辑。
android
冬田里的一把火34 小时前
[Android]导航栏中插入电源菜单
android
星途码客4 小时前
SQLyog中DELIMITER执行存储过程时出现的前置缩进问题
android