十七、自定义进度对话框
来源:互联网 发布:中信高端手机炒股软件 编辑:程序博客网 时间:2024/05/22 12:17
当用户进行需要等待一段时间的操作的时候,我们需要给用户一个等待的反馈,不然用户会以为你的APP卡死了,一般会用一个圆圈来转,但是系统提供的圆圈实在是太丑,所以我自定义一个进度对话框。
进度对话框由两部分组成,一个转的圆圈,和提示文字,布局如下:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/dialog_bg" android:padding="5dp" android:gravity="center_vertical"> <ImageView android:id="@+id/iv_progress" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/loading"/> <TextView android:id="@+id/tv_msg" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="5dp" android:layout_marginRight="5dp" android:text="加载中..." android:textColor="@color/font_white" android:textSize="@dimen/font_smaller"/></LinearLayout>自定义进度对话框代码:
public class CustomProgressDialog extends BaseDialog{ private ImageView progressImageView; private TextView msgTextView; private Animation anim; /** * * @param context * @param isCancelOutside 点击界外关闭dialog * @param canBack 返回键关闭dialog */ public CustomProgressDialog(Context context, boolean isCancelOutside, final boolean canBack) { super(context); if (view == null) { view = LayoutInflater.from(context).inflate(R.layout.dialog_progress, null); } setContentView(view); initView(); setCanceledOnTouchOutside(isCancelOutside); setOnKeyListener(new OnKeyListener() { @Override public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_BACK && !canBack) { // 过滤返回键 return true; } return false; } }); setOnShowListener(new OnShowListener() { @Override public void onShow(DialogInterface dialog) { // 开始旋转动画 progressImageView.startAnimation(anim); } }); } private void initView() { progressImageView = (ImageView) view.findViewById(R.id.iv_progress); msgTextView = (TextView) view.findViewById(R.id.tv_msg); anim = AnimationUtils.loadAnimation(context, R.anim.dialog_rotate); } /** * 设置提示文字内容 * @param msg */ public void setMessage(String msg) { if (msg != null) { msgTextView.setText(msg); } }}这样自定义对话框的内容就完成了,这样显然逼格不够,我们做一个对话框管理类,安全显示和关闭对话框:
public class DialogManager { /** * 创建进度对话框 * @param context * @param isCancelOutside 点击界外关闭dialog * @param canBack 返回键关闭dialog * @return */ public static CustomProgressDialog createProgressDialog(Context context, boolean isCancelOutside, final boolean canBack) { return new CustomProgressDialog(context, isCancelOutside, canBack); } /** * 安全打开dialog * @param activity * @param dialog */ public static void showDialog(Activity activity, Dialog dialog) { if (!activity.isFinishing() && dialog!=null && !dialog.isShowing()) { dialog.show(); } } /** * 安全关闭dialog * @param dialog */ public static void dismissDialog(Dialog dialog) { if (dialog!=null && dialog.isShowing()) { dialog.dismiss(); } }}在activity中去打开对话框,并且设置提示文字内容:
CustomProgressDialog dialog = DialogManager.createProgressDialog(getActivity(), false, true);dialog.setMessage("上传中");DialogManager.showDialog(getActivity(), dialog);效果如下:
满心欢喜的以为我们做了一个逼格高大上的dialog,结果发现丑得掉渣,都是样式style惹的祸,我们在style文件中自定义dialog样式:
<style name="CommonDialog" parent="@android:style/Theme.Dialog"> <item name="android:windowFrame">@null</item> <item name="android:windowIsFloating">true</item> <item name="android:windowIsTranslucent">true</item> <item name="android:windowNoTitle">true</item> <item name="android:background">@null</item> <item name="android:windowFullscreen">true</item> <item name="android:windowBackground">@color/transparent_color</item> <item name="android:backgroundDimEnabled">false</item> <item name="android:windowAnimationStyle">@null</item></style>在我们的dialog的基类文件BaseDialog去加载样式文件:
public BaseDialog(Context context) { super(context, R.style.CommonDialog);}
好了,再来看看效果吧:
参考:
http://blog.csdn.net/wwj_748/article/details/24592747
http://www.android100.org/html/201304/24/2282.html
1 0
- 十七、自定义进度对话框
- 自定义进度动画对话框
- 透明自定义对话框以及进度对话框的实现
- 透明自定义对话框以及进度对话框的实现
- 进度对话框
- Android开发系列(二十七):使用ProgressDialog创建进度对话框
- Android开发之自定义圆角矩形进度对话框
- android界面之美---自定义网络请求进度加载对话框
- 自定义进度
- Android 保存进度对话框
- 如何实现进度对话框
- QT 等待对话框/进度--
- Android进度对话框(ProgressDialog)
- 进度对话框 QProgressDialog
- QT 等待对话框/进度
- android 进度对话框 圈
- android 水平进度对话框
- 圆形进度对话框
- [HDU1159]-Common Subsequence
- Objective-C——关于Objective-C
- jsp数据库基础之--从MySQL数据库中删除数据
- 获取手势上的tag值
- javascript模版模式
- 十七、自定义进度对话框
- nodejs 学习 - 简单爬虫
- 解决 jsp表单提交中文乱码问题!
- Java - Collection
- 手动创建VS的DEV控件工具栏
- 网络连接状态检测:RealReachability
- 将基于spring的json字符串转换为Object对象
- 深度思考和学习
- 源码下载网站