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(); }
源码下载
点击打开链接
相关文章推荐
- 解密短信木马为何屡杀不尽--android手机短信木马的攻与防
- android模糊效果blur
- Android内核解读-Android系统的开机启动过程
- android-修改TextView中部分文字的颜色
- android 自定义dialog弹出和消失动画
- Android复制粘贴到剪贴板
- Android ListView滑动过程中控件显示重复/错误问题之原理分析及解决方案
- Android数据存储之SharePreference
- Activity生命周期之横竖屏切换
- Android高效加载大图、多图解决方案,有效避免程序OOM
- Android中ListView数据绑定和获取内部控件
- Android中数据存储之sqlite
- 野人学Android基础篇之异步任务第一课--AsyncTask原理简介及例子
- Mac下AndroidStudio常用快捷键整理
- Android PopupWindow
- Android 基于ShareSDK的Facebook第三方登录
- android Log工具框架,LogUtils让你摆脱TAG的魔爪
- android Log工具框架,LogUtils让你摆脱TAG的魔爪
- Android Studio无法找到tool.jar解决方法!
- Android Studio无法找到tool.jar解决方法!