为什么写这个博客。。因为有个项目用到ProgressDialog,原生的太丑了。维护起来太麻烦,本想在网上随便下载一下用一下;可是竟然要我5积分,囊中羞涩,随意自己来吧,本来也挺简单的。
首先先布局文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="wrap_content" android:paddingBottom="20dp" android:paddingLeft="30dp" android:paddingRight="30dp" android:paddingTop="20dp" android:background="@drawable/bg_progress" android:layout_height="wrap_content"> <ProgressBar android:layout_width="38dp" android:layout_height="38dp" android:layout_gravity="center_horizontal" /> <TextView android:id="@+id/tv_progress_message" android:text="加载中..." android:layout_marginTop="15dp" android:textColor="@android:color/white" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>然后是自定义Dialog
public class ProgressDialog { private Context context; private TextView tv_message; private Dialog dialog; public ProgressDialog(Context context) { this.context = context; dialog=create(context); } private Dialog create(final Context context){ View view = View.inflate(context, R.layout.dialog_progress_v, null); tv_message= (TextView) view .findViewById(R.id.tv_progress_message); Dialog dialog=new Dialog(context, R.style.ProgressDialog); dialog.setContentView(view); dialog.setCancelable(true); return dialog; } public void setMessage(String message){ if(tv_message!=null){ tv_message.setText(message); } } public void show(){ if(dialog!=null){ dialog.show(); } } public void dismiss(){ if(dialog!=null){ dialog.dismiss(); } } }最后是调用
ProgressDialog progressDialog=new ProgressDialog(this); progressDialog.setMessage("普通..."); progressDialog.show();
看下效果
发现ProgressBar颜色并不好看,所以又自定义写个颜色,放在drawable下:
<?xml version="1.0" encoding="utf-8"?> <animated-rotate xmlns:android="http://schemas.android.com/apk/res/android" android:pivotX="50%" android:pivotY="50%" android:fromDegrees="0" android:toDegrees="360"> <shape android:shape="ring" android:innerRadiusRatio="3" android:thicknessRatio="8" android:useLevel="false"> <gradient android:type="sweep" android:useLevel="false" android:startColor="#51D6E8" android:centerColor="#FB890F" android:centerY="0.50" android:endColor="#10AB09" /> </shape> </animated-rotate>
布局中修改,加入颜色:
<ProgressBar android:layout_width="38dp" android:layout_height="38dp" android:layout_gravity="center_horizontal" android:indeterminateDrawable="@drawable/progress_color" />效果如下: