码迷,mamicode.com
首页 > 移动开发 > 详细

安卓 自定义AlertDialog对话框(加载提示框)

时间:2016-06-27 17:13:04      阅读:173      评论:0      收藏:0      [点我收藏+]

标签:

AlertDialog有以下六种使用方法:

一、简单的AlertDialog(只显示一段简单的信息)

二、带按钮的AlertDialog(显示提示信息,让用户操作)

三、类似ListView的AlertDialog(展示内容)

四、类似RadioButton的AlertDialog(让用户选择,单选)

五、类似CheckBox的AlertDialog(让用户多选)

六、自定义View的AlertDialog(当以上方式满足不了你的需求,就要自定义了)

 

这里写的就是第六种用法,效果图如下(效果类似与IOS中的MBProgressHUD)

技术分享

 

具体实现如下:

.java代码

// 自定义弹出框,框内放入图片,图片设置旋转动画
AlertDialog alert_progress = new AlertDialog.Builder(当前activity.this).create();
alert_progress.show();
alert_progress.setCancelable(false); // 点击背景时对话框不会消失
// alert_progress.dismiss(); // 取消对话框
Window window = alert_progress.getWindow();
window.setContentView(R.layout.alert_dialog_progress_view); //加载自定义的布局文件
WindowManager.LayoutParams wm = window.getAttributes();
wm.width = 250; // 设置对话框的宽
wm.height = 200; // 设置对话框的高
wm.alpha = 0.5f; // 对话框背景透明度
wm.dimAmount = 0.6f; // 遮罩层亮度
window.setAttributes(wm);

ImageView img = (ImageView)window.findViewById(R.id.progress_bar); // 获取布局文件中的ImageView控件
img.setBackgroundResource(R.drawable.loading_one); // 设置图片,也可在布局文件中设置

// 设置旋转动画
Animation tranfrom = new RotateAnimation(0,359,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);(359:旋转角度(可自调),若为360会有卡顿,正数为顺势针旋转,负数为逆时针)
tranfrom.setDuration(2000); // 旋转速度
tranfrom.setFillAfter(true);
tranfrom.setRepeatCount(-1); // -1为一只旋转,若10,则旋转10次设定的角度后停止
// tranfrom.cancel(); // 取消动画
img.setAnimation(tranfrom);

布局代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorBlack">

<ImageView
android:id="@+id/progress_bar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"/>

<TextView
android:text="正在加载..."
android:layout_width="match_parent"
android:layout_height="20dp"
android:textColor="@color/colorWhite"
android:layout_gravity="center"
android:gravity="center"/>

</LinearLayout>

附:旋转背景图

技术分享

 


 

安卓 自定义AlertDialog对话框(加载提示框)

标签:

原文地址:http://www.cnblogs.com/sk-sp/p/5620258.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!