android 自定义progressDialog 之二

首先附上效果图


1、自定义dialog背景

[html]  view plain  copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <shape xmlns:android="http://schemas.android.com/apk/res/android" >  
  3.   
  4.     <!-- 边角的圆弧半径 -->  
  5.     <corners android:radius="10dp" />  
  6.   
  7.     <!-- 实心填充 -->  
  8.     <solid android:color="#ff000000" />  
  9.   
  10.     <!-- 描边:一般大小都是1dp -->  
  11.     <stroke  
  12.         android:width="1dp"  
  13.         android:color="#ff303030" />  
  14. </shape>  
2、自定义dialog布局

[html]  view plain  copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="wrap_content"  
  4.     android:layout_height="wrap_content"  
  5.     android:background="@drawable/dialog_bg"  
  6.     android:paddingBottom = "10dp"  
  7.     android:paddingTop = "10dp"  
  8.     android:paddingLeft="30dp"  
  9.     android:paddingRight="30dp"  >  
  10.   
  11.     <TextView  
  12.         android:id="@+id/progressdialog_tv_title"  
  13.         android:layout_width="wrap_content"  
  14.         android:layout_height="wrap_content"  
  15.         android:layout_alignParentTop="true"  
  16.         android:layout_centerHorizontal="true"  
  17.         android:text="载入中"  
  18.         android:textColor="#ffe0e0e0"  
  19.         android:textAppearance="?android:attr/textAppearanceLarge" />  
  20.       
  21.     <ProgressBar  
  22.         android:id="@+id/progressdialog_pb"  
  23.         style="?android:attr/progressBarStyleHorizontal"  
  24.         android:layout_width="match_parent"  
  25.         android:layout_height="12dp"  
  26.         android:layout_marginTop="12dp"  
  27.         android:layout_below="@id/progressdialog_tv_title"  
  28.         android:max="100"  
  29.         android:progress="0"  
  30.         android:progressDrawable="@drawable/progressbar_color"/>  
  31.   
  32.     <TextView  
  33.         android:id="@+id/progressdialog_tv_progress"  
  34.         android:layout_width="wrap_content"  
  35.         android:layout_height="wrap_content"  
  36.         android:layout_centerHorizontal="true"  
  37.         android:layout_below="@id/progressdialog_pb"  
  38.         android:layout_marginTop="4dp"  
  39.         android:text="0%"  
  40.         android:textColor="#ffe0e0e0"  
  41.         android:textAppearance="?android:attr/textAppearanceSmall" />  
  42.   
  43. </RelativeLayout>  



3、重写Dialog
[java]  view plain  copy
  1. package com.view;  
  2.   
  3. import android.app.Dialog;  
  4. import android.content.Context;  
  5. import android.os.Bundle;  
  6. import android.os.Handler;  
  7. import android.os.Message;  
  8. import android.view.LayoutInflater;  
  9. import android.view.View;  
  10. import android.view.ViewGroup.LayoutParams;  
  11. import android.view.Window;  
  12. import android.widget.ProgressBar;  
  13. import android.widget.TextView;  
  14. import com.example.tzq.R;  
  15.   
  16. public class MyProgressDialog extends Dialog {  
  17.     private Context context;  
  18.     private String title;  
  19.     private ProgressBar pb;  
  20.     private TextView tv_progress;  
  21.     private TextView tv_title;  
  22.     private Handler updateHandler;  
  23.       
  24.     public MyProgressDialog(Context context, String title) {  
  25.         super(context);  
  26.         // TODO Auto-generated constructor stub  
  27.         this.context = context;  
  28.         this.title = title;  
  29.           
  30.     }  
  31.   
  32.     @Override  
  33.     protected void onCreate(Bundle savedInstanceState) {  
  34.         // TODO Auto-generated method stub  
  35.         super.onCreate(savedInstanceState);  
  36.   
  37.         LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);  
  38.         View view = inflater.inflate(R.layout.dialog_progress, null);  
  39.         setContentView(view, new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));  
  40.         pb = (ProgressBar) findViewById(R.id.progressdialog_pb);  
  41.         tv_progress = (TextView) findViewById(R.id.progressdialog_tv_progress);  
  42.         tv_title = (TextView) findViewById(R.id.progressdialog_tv_title);  
  43.         tv_title.setText(title);  
  44.         getWindow().setBackgroundDrawableResource(android.R.color.transparent); // 去掉边角  
  45. //      getWindow().setBackgroundDrawable(new BitmapDrawable());  
  46.           
  47.         updateHandler = new Handler() {  
  48.               
  49.             @Override  
  50.             public void handleMessage(Message msg) {  
  51.                 // TODO Auto-generated method stub  
  52.                 if(msg.what == 1) {  
  53.                     int progress = (Integer)msg.obj;  
  54.                     String s = progress + "%";  
  55.                     tv_progress.setText(s);  
  56.                     pb.setProgress((int)progress);  
  57.                 }  
  58.             }  
  59.         };  
  60.     }  
  61.       
  62.     public void setProgress(double progress) {  
  63.         if(progress > 100) progress = 100;  
  64.           
  65.         updateHandler.obtainMessage(1, (int)progress).sendToTarget();  
  66.     }  
  67.       
  68.     @Override  
  69.     public void show() {  
  70.         // TODO Auto-generated method stub  
  71.         requestWindowFeature(Window.FEATURE_NO_TITLE); // 去掉标题  
  72.         super.show();  
  73.     }  
  74. }  
4、在Activity中使用

显示dialog

[java]  view plain  copy
  1. MyProgressDialog mProgressDialog = new MyProgressDialog(PlayActivity.this"载入中");  
  2. mProgressDialog.show();  

 
 
 
修改进度条 
[java]  view plain  copy
  1. mProgressDialog.setProgress(progress);  
取消显示
[java]  view plain  copy
  1. mProgressDialog.dismiss();  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值