Android自定义ProgressDialog
jopen
11年前
MainActivity如下:
package cn.testprogressdialog; import android.app.Activity; import android.app.Dialog; import android.content.Context; import android.content.DialogInterface; import android.content.DialogInterface.OnCancelListener; import android.os.Bundle; import android.view.Window; /** * Demo描述: * 自定义ProgressDialog * * 备注说明: * 在此处progressDialog.setOnCancelListener(){} * 并没有起到按下back键盘使风火轮消失的作用. * 因为在initDialog(Context context)中设置了 * dialog.setCancelable(false); * 若需要此功能,注释掉该句代码即可. */ public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Dialog progressDialog=initDialog(MainActivity.this); progressDialog.setOnCancelListener(new OnCancelListener() { @Override public void onCancel(DialogInterface dialog) { dialog.dismiss(); //其余业务逻辑 } }); progressDialog.show(); } public Dialog initDialog(Context context) { Dialog dialog = new DialogSubClass(MainActivity.this, R.style.dialog); dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); dialog.setCanceledOnTouchOutside(false); dialog.setCancelable(false); return dialog; } }
DialogSubClass如下:
package cn.testprogressdialog; import android.app.Dialog; import android.content.Context; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; public class DialogSubClass extends Dialog { public DialogSubClass(Context context, int theme) { super(context, theme); } protected DialogSubClass(Context context, boolean cancelable,OnCancelListener cancelListener) { super(context, cancelable, cancelListener); } public DialogSubClass(Context context) { super(context); } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); View view = LayoutInflater.from(getContext()).inflate(R.layout.dialoglayout, null); setContentView(view); } @Override public void show() { super.show(); } @Override public void dismiss() { super.dismiss(); } public void setMsg(String txt) { } }
dialoglayout.xml如下:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="wrap_content" android:layout_height="wrap_content" > <ProgressBar android:id="@+id/progressBar" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="loading" android:textSize="20sp" android:layout_below="@id/progressBar" android:layout_marginTop="-5dip" android:layout_centerHorizontal="true" android:textColor="@android:color/black" /> </RelativeLayout>
styles.xml如下:
<resources> <style name="AppBaseTheme" parent="android:Theme.Light"></style> <style name="AppTheme" parent="AppBaseTheme"></style> <style name="dialog" parent="@android:style/Theme.Dialog"> <item name="android:windowFrame">@null</item> <item name="android:windowIsFloating">true</item> <item name="android:windowIsTranslucent">true</item> <item name="android:windowNoTitle">true</item> <item name="android:windowBackground">@android:color/transparent</item> <item name="android:backgroundDimEnabled">true</item> <item name="android:windowContentOverlay">@null</item> </style> </resources>
main.xml如下:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="测试自定义ProgressDialog" android:layout_centerHorizontal="true" android:textSize="24sp" /> </RelativeLayout>