public static Dialog createLoadingDialog(Context context){
LayoutInflater inflater = LayoutInflater.from(context);
View v = inflater.inflate(R.layout.loading_dialog, null);// 得到加载view
LinearLayout layout = (LinearLayout) v.findViewById(R.id.dialog_view);// 加载布局
// main.xml中的ImageView
ImageView spaceshipImage = (ImageView) v.findViewById(R.id.imgLoading);
SceneAnimation s = new SceneAnimation(spaceshipImage,imglist,1);
// Glide.with(context).load(R.drawable.et_loader).asGif().diskCacheStrategy(DiskCacheStrategy.SOURCE).into(spaceshipImage);
Dialog loadingDialog = new Dialog(context,R.style.loadingDialogStyle);// 创建自定义样式dialog
loadingDialog.setCancelable(true);// 不可以用“返回键”取消
loadingDialog.setContentView(layout, new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.FILL_PARENT,
LinearLayout.LayoutParams.FILL_PARENT));// 设置布局
return loadingDialog;
}
这是我对话框的代码;
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
dialog.dismiss();
}
@Override
protected void onPreExecute() {
super.onPreExecute();
dialog = LoadingDialog.createLoadingDialog(activity);
dialog.show();
}
Don’t call UI in child thread
Try this
I want to achieve such an effect, when I request the api, make a loading effect. Now my loading box doesn't show up.
This is my request api class (NetworkTask), which inherits AsyncTask; I encapsulate all requests here.
This is my code for calling NetworkTask. The values passed by the constructor are the api name to be called and the current activity (Activity).
onPostExecute and onPreExecute are both called, and the dialog also has a value;
I heard that child threads cannot call ui? Is it because of this problem? So I can't call the dialog?
The following is the layout of my dialog box and the java code of the dialog box
There is an overridden method onProgressUpdate, and the dialog box is written in it