显示和隐藏进度条dialog
@Override
public void showLoading() {
hideLoading();
mProgressDialog = UiTools.showLoadingDialog(this, R.layout.common_loading);
}
@Override
public void hideLoading() {
if (mProgressDialog != null && mProgressDialog.isShowing()) {
mProgressDialog.cancel();
}
}
方案一:只有进度条
mProgressDialog = showLoadingDialog(this, R.layout.common_loading);
/** * 创建进度条dialog
* * @param context
* @param layoutRes 加载布局
* @return ProgressDialog
*/
public static ProgressDialog showLoadingDialog(Context context, @LayoutRes int layoutRes) {
ProgressDialog progressDialog = new ProgressDialog(context);
progressDialog.show();
//背景为透明
if (progressDialog.getWindow() != null) {
progressDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
progressDialog.setContentView(layoutRes);
progressDialog.setIndeterminate(true);
progressDialog.setCancelable(true);
progressDialog.setCanceledOnTouchOutside(false);
return progressDialog;
}
common_loading 布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<ProgressBar
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerInParent="true"
android:indeterminateTintMode="src_atop"
android:indeterminateTint="@color/colorPrimary"/>
<!--颜色可自定义-->
</LinearLayout>
方案二:进度条 下面加文字。动态设置文字状态
mProgressDialog = showLoadingDialog(this);
/** * 创建进度条dialog * * @param context * @return ProgressDialog */ public static ProgressDialog showLoadingDialog(Context context) { ProgressDialog progressDialog = new ProgressDialog(context); progressDialog.show(); //背景为透明 if (progressDialog.getWindow() != null) { progressDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT)); } LayoutInflater inflater = LayoutInflater.from(context); //加载loading_dialog.xml View v = inflater.inflate( R.layout.common_loading, null);// 得到加载view TextView textView = v.findViewById(R.id.tv_content); textView.setText("订单处理中"); //加载loading_dialog.xml progressDialog.setContentView(v); WindowManager.LayoutParams layoutParams = progressDialog.getWindow().getAttributes(); layoutParams.width = WindowManager.LayoutParams.MATCH_PARENT; layoutParams.height = WindowManager.LayoutParams.MATCH_PARENT; layoutParams.gravity = Gravity.CENTER; progressDialog.getWindow().setAttributes(layoutParams); progressDialog.setIndeterminate(true); progressDialog.setCancelable(true); progressDialog.setCanceledOnTouchOutside(false); return progressDialog; }
common_loading 布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ProgressBar android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerInParent="true"
android:indeterminateTintMode="src_atop"
android:indeterminateTint="@android:color/darker_gray"/>
<TextView
android:id="@+id/tv_content"
android:layout_marginTop="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="订单处理中"
android:textColor="#ffffff" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Loading..."
android:gravity="center"
android:textColor="#ffffff"/>
</LinearLayout>