现在的位置: 首页 > 综合 > 正文

Android透明无边框圆形进度条之最简单实现

2013年09月20日 ⁄ 综合 ⁄ 共 2314字 ⁄ 字号 评论关闭

http://blog.csdn.net/javasecret/article/details/7579698

        很多人在项目中做长时间操作时,比如访问web service后台数据,都想显示一个透明无边框的圆形进度条,如下图:

        不幸的是,Android系统自带的ProgressDialog,无论如何设置Theme、style,或者用java代码设置什么属性,边框都是去不掉的,至少我现在还不知道怎么去掉:

        怎么办?

        其实很简单,自定义一个ProgressDialog,加载自己的layout!

        先上layout xml:

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <ProgressBar xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="wrap_content"  
  4.     android:layout_height="wrap_content"  
  5.     android:interpolator="@android:anim/linear_interpolator" />  

        android:interpolator="@android:anim/linear_interpolator"这句话表示进度条动画是匀速的。

        再来定义一个CustomProgressDialog:

  1. public class CustomProgressDialog extends ProgressDialog{  
  2.   
  3.     public CustomProgressDialog(Context context) {  
  4.         super(context);  
  5.     }  
  6.       
  7.     public CustomProgressDialog(Context context, int theme) {  
  8.         super(context, theme);  
  9.     }  
  10.   
  11.     @Override  
  12.     protected void onCreate(Bundle savedInstanceState) {  
  13.         super.onCreate(savedInstanceState);  
  14.         setContentView(R.layout.dialog_progress);  
  15.     }  
  16.       
  17.     public static CustomProgressDialog show(Context ctx){  
  18.         CustomProgressDialog d = new CustomProgressDialog(ctx);  
  19.         d.show();  
  20.         return d;  
  21.     }  
  22. }  

        至于怎么用这个Dialog,就不用我教了吧。new一个CustomProgressDialog实例,然后调用继承来的show()方法也行,直接使用我们上面定义的静态show()方法也可,看个人偏好。

        有几点需要说明一下:

        1. 也可以继承AlertDialog,效果一样。

        2. 虽然我们在onCreate()方法里已经加载了自己的layout,但并不妨碍调用基类的方法,设置title、content,效果和直接用基类ProgressDialog一样,无视自定义。

        easy?本文只是提供一个最简单实现,相信已经能满足大部分的需求了,更深入的东西自己摸索去吧。

 

抱歉!评论已关闭.