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);}@Overrideprotected 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对象拿到背景显示的AnimationDrawablemAnimation = (AnimationDrawable) mImageView.getBackground();// 为了防止在onCreate方法中只显示第一帧的解决方案之一mImageView.post(new Runnable() {@Overridepublic 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();}


源码下载 点击打开链接
















更多相关文章

  1. 浅谈Java中Collections.sort对List排序的两种方法
  2. Python list sort方法的具体使用
  3. python list.sort()根据多个关键字排序的方法实现
  4. 工具:Android本地代码生成器
  5. android 动画
  6. android settings命令
  7. Android(安卓)Studio下“Error:Could not find com.android.tool
  8. Android隐藏标题栏,全屏显示
  9. android中判断网络是否连接

随机推荐

  1. android 所有布局属性和UI控件
  2. android ListView没有数据时信息显示
  3. 【Android开发学习01】与Android实体设备
  4. Android中RelativeLayout各个属性的含义
  5. 使用android兼容包android-support-v4.ja
  6. android uiautomator自己主动化測试
  7. Android内存管理机制
  8. Android多分辨率适配框架使用指南
  9. Dagger 2 在 Android(安卓)上的使用(四)
  10. Android自己主动化測试解决方式