android 自定义progressDialog

来源:互联网 发布:单片机招聘 编辑:程序博客网 时间:2024/05/29 03:02

首先附上效果图




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. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="wrap_content"  
  4.     android:layout_height="wrap_content"  
  5.     android:orientation="vertical"  
  6.     android:background="@drawable/dialog_bg"  
  7.     android:paddingBottom="20dp"  
  8.     android:paddingLeft="30dp"  
  9.     android:paddingRight="30dp"  
  10.     android:paddingTop="20dp" >  
  11.   
  12.     <ProgressBar  
  13.         android:id="@+id/progressBar1"  
  14.         android:layout_width="wrap_content"  
  15.         android:layout_height="wrap_content"  
  16.         android:layout_gravity="center_horizontal" />  
  17.   
  18.     <TextView  
  19.         android:id="@+id/tv_dialogmsg"  
  20.         android:layout_width="wrap_content"  
  21.         android:layout_height="wrap_content"  
  22.         android:layout_gravity="center_horizontal"  
  23.         android:text="加载中..."  
  24.         android:textColor="#ffa0a0a0"  
  25.         android:textAppearance="?android:attr/textAppearanceSmall" />  
  26.   
  27. </LinearLayout>  
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.view.LayoutInflater;  
  7. import android.view.View;  
  8. import android.view.ViewGroup.LayoutParams;  
  9. import android.view.Window;  
  10. import android.widget.TextView;  
  11.   
  12. import com.example.tzq.R;  
  13.   
  14. public class MyLoadDialog extends Dialog {  
  15.       
  16.     private Context context;  
  17.     private String text;  
  18.   
  19.     public MyLoadDialog(Context context, String text) {  
  20.         super(context);  
  21.         // TODO Auto-generated constructor stub  
  22.         this.context = context;  
  23.         this.text = text;  
  24.     }  
  25.   
  26.     @Override  
  27.     protected void onCreate(Bundle savedInstanceState) {  
  28.         // TODO Auto-generated method stub  
  29.         super.onCreate(savedInstanceState);  
  30.         LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);  
  31.         View view = inflater.inflate(R.layout.dialog_load, null);  
  32.         setContentView(view, new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));  
  33.           
  34.         //      getWindow().setBackgroundDrawable(new BitmapDrawable());  
  35.         getWindow().setBackgroundDrawableResource(android.R.color.transparent); // 去掉边角  
  36.           
  37.         if(text != null) {  
  38.             TextView tv = (TextView) findViewById(R.id.tv_dialogmsg);  
  39.             tv.setText(text);  
  40.         }  
  41.     }  
  42.     @Override  
  43.     public void show() {  
  44.         // TODO Auto-generated method stub  
  45.         requestWindowFeature(Window.FEATURE_NO_TITLE); // 去掉标题<pre name="code" class="java">  
 super.show();}}

4、在Activity中调用

显示Dialog

[java] view plain copy
  1. MyLoadDialog loadDialog = new MyLoadDialog(LoginActivity.this"登录中,请稍后...");  
  2. loadDialog.show();  
取消Dialog
[java] view plain copy
  1. loadDialog.dismiss()
原创粉丝点击