自定义ProgressDialog 博客2

来源:互联网 发布:淘宝客服需要会哪些 编辑:程序博客网 时间:2024/05/20 21:47

算是半改这篇文章吧,稍作修改,有不足之处,还请指正

纯转载不运行的事我不干,我的博客都是能运行的东西,不想让人看了没结果

http://blog.csdn.net/yeqishi/article/details/7622023

看看效果图


Android系统自带的ProgressDialog样式的确不太好看,我们可以自己定义它的样式,下面看看实现


1.style.xml  progressDialog继承与Dialog,先设置一下progressDialog的风格,设置背景图片。

[html] view plaincopy
  1. <style name="CustomDialog" parent="@android:style/Theme.Dialog">  
  2.         <item name="android:windowFrame">@null</item>  
  3.         <item name="android:windowIsFloating">true</item>  
  4.         <item name="android:windowContentOverlay">@null</item>  
  5.         <item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item>  
  6.         <item name="android:windowSoftInputMode">stateUnspecified|adjustPan</item>  
  7.     </style>  
  8.   
  9.     <style name="CustomProgressDialog" parent="@style/CustomDialog">  
  10.         <item name="android:windowBackground">@drawable/toast_frame</item>  
  11.         <item name="android:windowNoTitle">true</item>  
  12.     </style>  

2.customprogressdialog.xml文件,定义自己的布局,由于我的需求只需要一个进度条以及一串显示的内容,所以布局比较简单

[html] view plaincopy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:orientation="horizontal"  
  6.     android:padding="8dp" >  
  7.   
  8.     <ProgressBar  
  9.         android:id="@+id/loadingImageView"  
  10.         android:layout_width="wrap_content"  
  11.         android:layout_height="wrap_content"  
  12.         android:layout_marginRight="15dp"  
  13.         android:indeterminateDrawable="@drawable/progress_medium" />  
  14.   
  15.     <TextView  
  16.         android:id="@+id/id_tv_loadingmsg"  
  17.         android:layout_width="wrap_content"  
  18.         android:layout_height="wrap_content"  
  19.         android:layout_gravity="center_vertical"  
  20.         android:textColor="@android:color/white"  
  21.         android:textSize="18dp" />  
  22.   
  23. </LinearLayout>  

3.progress_medium.xml文件.旋转效果。

[html] view plaincopy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <animated-rotate xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:drawable="@drawable/spinner_black_32"  
  4.     android:fromDegrees="0"  
  5.     android:pivotX="50.0%"  
  6.     android:pivotY="50.0%"  
  7.     android:toDegrees="360" />  

4.CustomProgressDialog.java文件,这个是就是我们最终需要使用的progressDialog了。

[java] view plaincopy
  1. package com.xxx.view;  
  2.   
  3. import com.xxx.activity.R;  
  4.   
  5. import android.app.Dialog;  
  6. import android.content.Context;  
  7. import android.view.Gravity;  
  8. import android.widget.TextView;  
  9.   
  10. public class CustomProgressDialog extends Dialog {  
  11.     public CustomProgressDialog(Context context, String strMessage) {  
  12.         this(context, R.style.CustomProgressDialog, strMessage);  
  13.     }  
  14.   
  15.     public CustomProgressDialog(Context context, int theme, String strMessage) {  
  16.         super(context, theme);  
  17.         this.setContentView(R.layout.customprogressdialog);  
  18.         this.getWindow().getAttributes().gravity = Gravity.CENTER;  
  19.         TextView tvMsg = (TextView) this.findViewById(R.id.id_tv_loadingmsg);  
  20.         if (tvMsg != null) {  
  21.             tvMsg.setText(strMessage);  
  22.         }  
  23.     }  
  24.   
  25.     @Override  
  26.     public void onWindowFocusChanged(boolean hasFocus) {  
  27.   
  28.         if (!hasFocus) {  
  29.             dismiss();  
  30.         }  
  31.     }  
  32. }  


我修改的部分也就主要是这里,之前作者用静态方法去构造ProgressDialog,context无法释放,下面是我修改后的代码


在Activity里面构造showProgressDialog:我写在自己的父类里

[java] view plaincopy
  1. public void showProgress(int resID, boolean canBack) {  
  2.         if (progressDialog != null) {  
  3.             progressDialog.cancel();  
  4.         }  
  5.         progressDialog = new CustomProgressDialog(activity, getResources()  
  6.                 .getString(resID));  
  7.         progressDialog.show();  
  8.     }  

子类继承调下这个方法就show出来了。
0 0
原创粉丝点击