自定义View完成网络加载时提示的Diaglog
public class LoadDialog extends Dialog {
private LinearLayout linearLayout;
private ProgressBar mBar;
private TextView mTv;
public LoadDialog(@NonNull Context context) {
super(context);
//点击外部不消失
setCancelable(false);
getWindow().setDimAmount(0);
getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
getWindow().setContentView(initView());
}
private LinearLayout initView() {
//设置父容器
linearLayout = new LinearLayout(getContext());
linearLayout.setOrientation(LinearLayout.VERTICAL);
linearLayout.setGravity(Gravity.CENTER_VERTICAL);
linearLayout.setBackgroundResource(R.drawable.dialog_bg);
linearLayout.setAlpha(0.5f);
//初始化子控件
LinearLayout.LayoutParams tvParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,0);
tvParams.gravity = Gravity.CENTER;
tvParams.weight = 2;
LinearLayout.LayoutParams barParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,0);
barParams.gravity = Gravity.CENTER;
barParams.weight = 8;
mBar = new ProgressBar(getContext());
mBar.setLayoutParams(barParams);
mTv = new TextView(getContext());
mTv.setText("正在加载,请稍候...");
mTv.setTextColor(Color.BLACK);
mTv.setGravity(Gravity.CENTER);
//添加到父容器
linearLayout.addView(mBar);
linearLayout.addView(mTv);
return linearLayout;
}
}
使用
LoadDialog loadDialog = new LoadDialog(this);
loadDialog.show();