androidの自定义加载对话框ProgressDialog
16lz
2021-12-04
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();}
源码下载 点击打开链接
更多相关文章
- 浅谈Java中Collections.sort对List排序的两种方法
- Python list sort方法的具体使用
- python list.sort()根据多个关键字排序的方法实现
- 工具:Android本地代码生成器
- android 动画
- android settings命令
- Android(安卓)Studio下“Error:Could not find com.android.tool
- Android隐藏标题栏,全屏显示
- android中判断网络是否连接