主要代码
import android.app.Dialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import cn.com.kspay.R;
/**
* 加载中dialog
* @author zhongzhengming
* @e-mail zhongzhengming@kspay.net
* @time 2017/12/19
*/
public class LoadingDialog {
private Dialog mLoadingDialog;
/**
* @param context
* @param message dialog中文字
*/
public LoadingDialog(Context context, String message) {
// 首先得到整个View
View view = LayoutInflater.from(context).inflate(
R.layout.dialog_loading, null);
// 获取整个布局
LinearLayout layout = (LinearLayout) view.findViewById(R.id.dialog_loading_view);
// 页面中显示文本
TextView loadingText = (TextView) view.findViewById(R.id.tipTextView);
// 显示文本
loadingText.setText(message);
// 创建自定义样式的Dialog
mLoadingDialog = new Dialog(context, R.style.loading_dialog);
// 设置返回键无效
mLoadingDialog.setCancelable(false);
mLoadingDialog.setContentView(layout, new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT));
}
public void showDialog() {
if (mLoadingDialog != null && !mLoadingDialog.isShowing()) {
mLoadingDialog.show();
}
}
public void cancelDialog() {
if (mLoadingDialog != null && mLoadingDialog.isShowing()) {
mLoadingDialog.dismiss();
}
}
}
布局文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/dialog_loading_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<LinearLayout
android:layout_width="150dp"
android:layout_height="110dp"
android:background="@drawable/loading_bg"
android:gravity="center"
android:orientation="vertical"
android:paddingBottom="10dp"
android:paddingLeft="21dp"
android:paddingRight="21dp"
android:paddingTop="10dp">
<ProgressBar
android:id="@+id/progressBar1"
android:layout_width="35dp"
android:layout_height="35dp"
android:layout_gravity="center_horizontal"
android:indeterminateBehavior="repeat"
android:indeterminateDrawable="@drawable/dialog_loading"
android:indeterminateOnly="true" />
<TextView
android:id="@+id/tipTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:text="加载中..."
android:textColor="#f0f0f0"
android:textSize="15sp" />
</LinearLayout>
</LinearLayout>```
网友评论