使用ProgressBar+AlertDialog实现ProgressDialog耗时进度圈效果
在android 26以后,ProgressDialog类就被标志为过时了,虽然还是可以继续使用,但是目前Google官方已经不再推荐使用这个类了,推荐使用ProgressBar来替代该类,所以在这里分享一种使用ProgressBar+AlertDialog实现ProgressDialog耗时进度圈的效果
1.通过在styles.xml文件中写一个style去除AlertDialog的背景
<!--自定义耗时对话框-->
<style name="CustomProgressDialog" parent="Theme.AppCompat.Dialog">
<!--此属性控制悬浮窗背景是否变暗-->
<item name="android:backgroundDimEnabled">false</item>
<item name="android:windowBackground">@android:color/transparent</item>
</style>
2.编写这个耗时对话框的布局文件custom_progress_dialog_view.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/progress_bg">
<ProgressBar
android:id="@+id/progressBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/tvTip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginTop="10dp"
android:layout_marginRight="20dp"
android:layout_marginBottom="10dp"
android:gravity="center"
android:text="加载中..."
android:textColor="@android:color/white"
android:textSize="16dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@id/progressBar" />
</android.support.constraint.ConstraintLayout>
3.耗时对话框的背景shape文件progress_bg.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<corners android:radius="8dp"/>
<solid android:color="#343134"/>
</shape>
4.接下来就是创建AlertDialog对话框,在这里将对话框的弹出与隐藏封装成一个工具类,调用起来可以更加方便
/**
* 耗时对话框工具类
*/
public class ProgressDialogUtil {
private static AlertDialog mAlertDialog;
/**
* 弹出耗时对话框
* @param context
*/
public static void showProgressDialog(Context context) {
if (mAlertDialog == null) {
mAlertDialog = new AlertDialog.Builder(context, R.style.CustomProgressDialog).create();
}
View loadView = LayoutInflater.from(context).inflate(R.layout.custom_progress_dialog_view, null);
mAlertDialog.setView(loadView, 0, 0, 0, 0);
mAlertDialog.setCanceledOnTouchOutside(false);
TextView tvTip = loadView.findViewById(R.id.tvTip);
tvTip.setText("加载中...");
mAlertDialog.show();
}
public static void showProgressDialog(Context context, String tip) {
if (TextUtils.isEmpty(tip)) {
tip = "加载中...";
}
if (mAlertDialog == null) {
mAlertDialog = new AlertDialog.Builder(context, R.style.CustomProgressDialog).create();
}
View loadView = LayoutInflater.from(context).inflate(R.layout.custom_progress_dialog_view, null);
mAlertDialog.setView(loadView, 0, 0, 0, 0);
mAlertDialog.setCanceledOnTouchOutside(false);
TextView tvTip = loadView.findViewById(R.id.tvTip);
tvTip.setText(tip);
mAlertDialog.show();
}
/**
* 隐藏耗时对话框
*/
public static void dismiss() {
if (mAlertDialog != null && mAlertDialog.isShowing()) {
mAlertDialog.dismiss();
}
}
}
5.接下来可以在调用的地方通过下面方法进行对话框的显示
//显示对话框
ProgressDialogUtil.showProgressDialog(DialogActivity.this);
//隐藏对话框
ProgressDialogUtil.dismiss();