自定义AlertDialog
2017-08-29 17:12
141 查看
自定义AlertDialog
先上效果图![](https://i.loli.net/2017/08/29/59a52e366b257.jpg)
这是我自定义的背景透明的,可全局使用的,有动画的AlertDialog
使用方法:
定义:loadingProgressDialog = new LoadingProgressDialog(this, “正在加载中…”, R.drawable.animation_loading);
显示:loadingProgressDialog.show();
隐藏:loadingProgressDialog.dismiss();
源码如下:
public class LoadingProgressDialog extends AlertDialog { /** * 动画的资源文件 */ private int mResId; /** * 对话框的标题 */ private String mLoadingTitle; private Context mContext; /** * 显示图片 */ private ImageView mImage; /** * 显示标题 */ private TextView mContent; /** * 动画 */ private AnimationDrawable mAnimation; public LoadingProgressDialog(Context context) { super(context); } public LoadingProgressDialog(Context context, int theme) { super(context, theme); } public LoadingProgressDialog(@NonNull Context context, boolean cancelable, @Nullable OnCancelListener cancelListener) { super(context, cancelable, cancelListener); } /** * 构造方法 * * @param context Context * @param content 标题 * @param resId 动画资源 */ public LoadingProgressDialog(Context context, String content, int resId) { super(context,R.style.loadingDialog); this.mContext = context; this.mLoadingTitle = content; this.mResId = resId; } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); initView(); initData(); } private void initView() { setContentView(R.layout.layout_loading); mImage = (ImageView) findViewById(R.id.iv_loading); mContent = (TextView) findViewById(R.id.tv_loading); //点击其他地方不能取消 setCanceledOnTouchOutside(false); //设置对话框的宽度为屏幕宽度的一般 Window window = getWindow(); if (window != null) { WindowManager.LayoutParams attributes = window.getAttributes(); attributes.width = DevicesUtil.getDeviceWidth(mContext).widthPixels / 2; window.setAttributes(attributes); } } private void initData() { //设置图片的资源 mImage.setBackgroundResource(mResId); //获取图片背景 mAnimation = (AnimationDrawable) mImage.getBackground(); mImage.post(new Runnable() { @Override public void run() { mAnimation.start(); } }); //设置标题 mContent.setText(mLoadingTitle); } }
自定义的步骤
设置布局设置宽高
设置控件上的内容
设置图片的动画
附动画源码:
<?xml version="1.0" encoding="utf-8"?> <animation-list xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/loading1" android:duration="100"/> <item android:drawable="@drawable/loading2" android:duration="100"/> <item android:drawable="@drawable/loading3" android:duration="100"/> <item android:drawable="@drawable/loading4" android:duration="100"/> </animation-list>
相关文章推荐
- AlertDialog使用和自定义
- AlertDialog自定义单选功能
- 【自定义AlertDialog】退出提示框
- Android UI中的自定义style和theme以及AlertDialog
- Android使用自定义AlertDialog(退出提示框)
- Android使用自定义AlertDialog(退出提示框)
- AlertDialog(自定义对话框)示例
- Android 自定义AlertDialog退出对话框
- Android之自定义AlertDialog和PopupWindow实现(仿微信Dialog)
- android自定义alertdialog的四角
- 自定义弹出AlertDialog弹出框
- 自定义 AlertDialog 主题的方法
- Android使用自定义AlertDialog(退出提示框)
- 自定义alertDialog
- Android使用自定义AlertDialog(退出提示框)
- Android实现自定义AlertDialog的自下向上的动画效果(并消除dialog边框)
- [原创] Android 自定义AlertDialog 去黑边终极解决方案(亲测有效!)
- AlertDialog中使用自定义View,view中的Button如何设置点击事件监听
- 自定义包含DatePicker的AlertDialog
- 鱼鱼Chen之学写自己的apk(四)灵活使用EditText的hint属性以及使用自定义的美观AlertDialog