开发中经常需要用到progressDialog,但自带的太挫了,自定义吧
效果图:
1,需要为Dialog定义一个布局文件,我这里定义一个waiting.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">
<TextView
android:id="@+id/waiting_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:text="请等待^.^"/>
<ImageView android:id="@+id/waiting_iv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/waiting_tv"
/>
</RelativeLayout>
2,dialog里面有个逐帧动画,定义一个animation-lis放于drawable文件夹
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="false">
<!-- oneshot false表示循环 -->
<item android:drawable="@drawable/comment_load1" android:duration="150"/>
<item android:drawable="@drawable/comment_load2" android:duration="150"/>
<item android:drawable="@drawable/comment_load3" android:duration="150"/>
<item android:drawable="@drawable/comment_load4" android:duration="150"/>
</animation-list>
3,自定义的dialog控件
public class WaitingDialog extends Dialog{
private TextView tv;
private ImageView iv;
private AnimationDrawable mAnimationDrawable;
public WaitingDialog(Context context, int theme) {
super(context, theme);
// TODO Auto-generated constructor stub
}
protected WaitingDialog(Context context, boolean cancelable,
OnCancelListener cancelListener) {
super(context, cancelable, cancelListener);
// TODO Auto-generated constructor stub
}
public WaitingDialog(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
View view = LayoutInflater.from(getContext()).inflate(R.layout.waiting, null);
tv = (TextView) view.findViewById(R.id.waiting_tv);
iv = (ImageView) view.findViewById(R.id.waiting_iv);
iv.setImageResource(R.drawable.waitting);
setContentView(view);
}
@Override
public void show() {
// TODO Auto-generated method stub
super.show();
mAnimationDrawable = (AnimationDrawable) iv.getDrawable();
mAnimationDrawable.start();
}
@Override
public void dismiss() {
// TODO Auto-generated method stub
super.dismiss();
mAnimationDrawable = (AnimationDrawable) iv.getDrawable();
mAnimationDrawable.stop();
}
public void setMsg(String txt) {
tv.setText(txt);
}
}
4,为dialog设置外观样式,有没有title啊,边框啊,什么的,不会写就参考Themes.xml这个文件
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- -->
<style name="WaitingDialog" parent="@android:style/Theme.Dialog">
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:windowNoTitle">true</item>
</style>
</resources>
5,在Activity调用,注意一下 传的是 Activity.this,如果传getBaseContext(),或者getApplicationContext()的话会报windowstoken异常
private void WaitingDialogTest() {
Dialog mDialog = new WaitingDialog(this, R.style.WaitingDialog);
mDialog.show();
}