您的位置:首页 > 其它

自定义的加载中弹出框,重写ProgessDialog

2014-09-19 14:49 357 查看
在子线程与主线程交互,或不同activity之间跳转过程中,progressdialog是用得比较多的,但是系统自带的ProgressDialog效果很丑,于是自己写了一个progressdialog。

先上效果:



首先,我们来写前台界面,涉及到两个文件,一个是布局文件:layout下的mprogressbar.xml;一个样式文件:drawable下的anim_rotate.xml,样式文件主要是让那个圆环的图片转起来。

mprogressbar.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:background="@drawable/common_corner_border_bg" 
    android:padding="10dip"
    android:gravity="center_vertical">
    
    <ProgressBar  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:layout_marginRight="15dp"  
        android:indeterminateDrawable="@drawable/anim_rotate" />  
    
    <TextView
        android:id="@+id/progress_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@color/color_666"
        android:text="加载中..." />

</LinearLayout>


ProgressBar中引用的android:indeterminateBrawable就是样式文件:drawable下的anim_rotate.xml。

amin_rotate.xml

<?xml version="1.0" encoding="utf-8"?>
<animated-rotate xmlns:android="http://schemas.android.com/apk/res/android"  
       android:drawable="@drawable/common_listview_spinner_black_48"  
       android:fromDegrees="0"  
       android:pivotX="50.0%"  
       android:pivotY="50.0%"  
       android:toDegrees="359" />
代码中的android:drawable就是圆环图片,下面的几行是让图片转动的设置,具体含义请自己查阅。

然后就是后台程序,因为是整个app通用的,所以先封装为一个名为mProgressbar.java的类

import android.app.ProgressDialog;
import android.content.Context;
import android.view.Window;
import android.widget.TextView;

/*
 * 因为ProgressDialog和AlertDialog都是继承自Dialog;所以这个地方 extends Dialog、AlertDialog、ProgressDialog都行
*/
public class mProgressbar extends ProgressDialog{
	
	public mProgressbar(Context context){
		super(context);
	}
	
	/*
	 * 自定义加载中弹出框
	*/
	public void showProgess(String message){	
		setCancelable(true);   //设置点击框外消失
		show();
		Window window = getWindow();
		window.setContentView(R.layout.mprogressbar);   //自定义的layout样式
		TextView progress_text = (TextView)window.findViewById(R.id.progress_text);
		progress_text.setText(message);
	}
	
}


调用的方法showProgress()的参数String message,就是自定义显示在旋转的圆环右边那几个字。

下面来看如何调用这个封装好的类

mProgressbar mprogressbar = new mProgressbar(v.getContext());
mprogressbar.showProgess("加载中...");
代码中,注意需要先import mProgressbar.java这个类
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: