您的位置:首页 > 移动开发 > Android开发

androidの自定义加载对话框ProgressDialog

2015-09-07 13:32 375 查看

androidの自定义加载对话框ProgressDialog

1. 自定义ProgressDialog如图所示





2 . 自定义组件,一般常见都是定义一个View类,继承自该组件看下源码:
public class CustomProgressDialog extends ProgressDialog {
private AnimationDrawable mAnimation;
private Context mContext;
private ImageView mImageView;
private String mLoadingTip;
private TextView mLoadingTv;
private int count = 0;
private String oldLoadingTip;
private int mResid;

public CustomProgressDialog(Context context, String content, int id,int theme) {
super(context, theme);
this.mContext = context;
this.mLoadingTip = content;
this.mResid = id;
setCanceledOnTouchOutside(true);
}

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
initView();
initData();
}

private void initView() {
setContentView(R.layout.progress_dialog);
mLoadingTv = (TextView) findViewById(R.id.loadingTv);
mImageView = (ImageView) findViewById(R.id.loadingIv);
}

private void initData() {
mImageView.setBackgroundResource(mResid);
// 通过ImageView对象拿到背景显示的AnimationDrawable
mAnimation = (AnimationDrawable) mImageView.getBackground();
// 为了防止在onCreate方法中只显示第一帧的解决方案之一
mImageView.post(new Runnable() {
@Override
public void run() {
mAnimation.start();
}
});
mLoadingTv.setText(mLoadingTip);

}
}
我们会看到先定义一个构造方法, 此方法用来调用时候用于传值。 调用完构造方法后,会执行onCreate(), 加载一个xml布局文件。同时为该xml布局
组件赋值。 看下xml文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="vertical" >

<ImageView
android:id="@+id/loadingIv"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

<TextView
android:id="@+id/loadingTv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/loadingIv"
android:layout_centerHorizontal="true"
android:text="正在加载中.."
android:textSize="20sp" />
</RelativeLayout>
这个布局文件其实很简单,就是一个图片跟一个TextView

在initData() 方法中,进行赋值情况,并执行动画。看起来很简单了。。这种自定义view ,结合xml 相对容易些。

最终就是调用即可
public void showmeidialog(){
dialog=new CustomProgressDialog(this, "正在加载中",R.anim.frame,R.style.TRANSDIALOG);
dialog.show();
}


源码下载
点击打开链接
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: