自定義 Dialog 載入進度,去除自定義Dialog的白色背景
阿新 • • 發佈:2018-11-09
去除自定義Dialog的白色背景:
mDialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
這句話就可以了,一定要記得 在show()前加;
下面附上完整程式碼:
public class MyProgressDialog { private static Dialog mDialog; public static void progressDialog(Context mContext) { if (null == mDialog || !mDialog.isShowing()) { OnKeyListener keyListener = new OnKeyListener() { @Override public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) { mDialog.dismiss(); } return false; } }; mDialog = new AlertDialog.Builder(mContext).create(); //去除dialog背景白色 mDialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent); //監聽返回 mDialog.setOnKeyListener(keyListener); //false:dialog彈出後會點選螢幕或物理返回鍵,dialog不消失 mDialog.setCancelable(true); //false:dialog彈出後會點選螢幕,dialog不消失;點選物理返回鍵dialog消失 mDialog.setCanceledOnTouchOutside(true); mDialog.show(); mDialog.setContentView(R.layout.loading_process_dialog_icon); } } public static void cancleProgress() { if (null != mDialog && mDialog.isShowing()) { mDialog.dismiss(); } } }
佈局檔案:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_gravity="center" android:orientation="horizontal" android:background="#00000000" android:padding="10dip" > <ProgressBar android:id="@+id/loading_process_dialog_progressBar" android:layout_width="@dimen/DIMEN_45DP" android:layout_height="@dimen/DIMEN_45DP" android:indeterminate="false" android:layout_centerInParent="true" android:indeterminateDrawable="@drawable/dialog_style_xml_icon" /> <TextView android:id="@+id/loading_process_dialog_text" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center" android:layout_marginTop="10dip" android:layout_below="@+id/loading_process_dialog_progressBar"/> </RelativeLayout>
資原始檔 dialog_style_xml_icon:
<?xml version="1.0" encoding="utf-8"?> <layer-list xmlns:android="http://schemas.android.com/apk/res/android" > <item> <rotate android:drawable="@drawable/new_progress_icon" android:fromDegrees="0.0" android:pivotX="50.0%" android:pivotY="50.0%" android:toDegrees="360.0" /> <set android:interpolator="@android:anim/linear_interpolator" > </set> </item> </layer-list>
圖片資源
調起載入框:
MyProgressDialog.progressDialog(this);
載入框消失:
MyProgressDialog.cancleProgress();
效果圖如下: