NewDialog.java
避免IllegalArgumentException
关于错误分析在下面
package com.youpinwallet.ypw.view; import android.app.Activity; import android.app.Dialog; import android.content.Context; import android.view.Gravity; import android.view.WindowManager; import com.youpinwallet.ypw.R; /* * 白色背景,垂直居中 * @author Administrator * */ public class NewDialog extends Dialog { Activity mParentActivity; public NewDialog(Context context, int theme) { super(context, theme); mParentActivity= (Activity) context; } public NewDialog(Context context) { super(context); } /** * 弹出自定义ProgressDialog * * @param context 上下文 * @param cancelable 是否按返回键取消 * @param cancelListener 按下返回键监听 * @return */ public static NewDialog create(Context context, boolean cancelable, OnCancelListener cancelListener) { NewDialog dialog = new NewDialog(context, R.style.Custom_Progress); dialog.setContentView(R.layout.progress_layout); // 按返回键是否取消 dialog.setCancelable(cancelable); // 监听返回键处理 dialog.setOnCancelListener(cancelListener); // 设置居中 dialog.getWindow().getAttributes().gravity = Gravity.CENTER; WindowManager.LayoutParams lp = dialog.getWindow().getAttributes(); // 设置背景层透明度 lp.dimAmount = 0.2f; dialog.getWindow().setAttributes(lp); // dialog.show(); return dialog; } @Override public void dismiss() { // 避免依附界面销毁跑出的异常java.lang.IllegalArgumentException: View=com.android.internal.policy.PhoneWindow$DecorView if (mParentActivity != null && !mParentActivity.isFinishing()) { super.dismiss(); //调用超类对应方法 } } }
NewDialogUtils.java
package com.qunarjie.qnj.utils; import android.content.Context; import com.qunarjie.qnj.view.NewDialog; //请稍等通用 public class NewDialogUtils { private static NewDialog dialog = null; public static void setNewDialog(Context context) { if (context != null) { if (dialog != null) { dialog.dismiss(); dialog = null; } if (dialog == null) { dialog = NewDialog.create(context, true, null); } dialog.show(); } } public static void dismissNewDialog() { if (dialog != null && dialog.isShowing()) { dialog.dismiss(); dialog = null; } } }
R.style.Custom_Progress
styles.xml
<!-- 自定义Progress --> <style name="Custom_Progress" parent="@android:style/Theme.Dialog"> <item name="android:windowFrame">@null</item> <item name="android:windowIsFloating">true</item> <item name="android:windowContentOverlay">@null</item> <item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item> <item name="android:windowSoftInputMode">stateUnspecified|adjustPan</item> <item name="android:windowBackground">@android:color/transparent</item> <item name="android:windowCloseOnTouchOutside">false</item> <item name="android:windowNoTitle">true</item> </style>
progress_layout.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:background="@drawable/shape_dialog_white" android:gravity="center_horizontal" android:orientation="vertical" android:paddingBottom="20dp" android:paddingLeft="30dp" android:paddingRight="30dp" android:paddingTop="20dp"> <ProgressBar android:layout_width="wrap_content" android:layout_height="wrap_content" android:indeterminateDrawable="@drawable/progressbar" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="@dimen/mdp_15" android:text="请稍等..." android:textColor="@color/color_text_dark" android:textSize="@dimen/msp_14" /> </LinearLayout>
shape_dialog_white.xml
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android"> <corners android:radius="5dp" /> <solid android:color="@color/white" /> </shape>
progressbar.xml
<?xml version="1.0" encoding="utf-8"?> <animated-rotate xmlns:android="http://schemas.android.com/apk/res/android" android:fromDegrees="0" android:pivotX="50%" android:pivotY="50%" android:toDegrees="3600"> <shape android:innerRadiusRatio="3" android:shape="ring" android:thicknessRatio="8" android:useLevel="false"> <gradient android:centerY="0.50" android:endColor="#12b7f5" android:startColor="#038fef" android:type="sweep" android:useLevel="false" /> </shape> </animated-rotate>