【Android UI设计】Dialog对话框详解(二)

上一篇我们介绍了Dialog的基本使用方法,【Android UI设计】Dialog对话框详解(一)今天继续介绍,废话不多说,今天主要实现ProgressDialog和透明Dialog两种效果,最后介绍一下github上的一个Dialog动画开源库,,里面包含多种动画特效,效果图如下:

一、ProgressDialog基本使用

1.ProgressDialog关键代码

mProgressDialog = new ProgressDialog(MainActivity.this);mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);// 设置ProgressDialog标题mProgressDialog.setTitle(“天天熬夜”);// 设置ProgressDialog提示mProgressDialog.setMessage(“想死的心都有了···”);// 设置ProgressDialog进度条的图标mProgressDialog.setIcon(R.drawable.dialog);// 如果设置为false就是滚动条的当前值自动在最小到最大值之间来回移动,形成这样一个动画效果,告诉别人“我正在工作”,但不能提示工作进度到哪个阶段。// 主要是在进行一些无法确定操作时间的任务时作为提示。而“明确”(true)就是根据你的进度可以设置现在的进度值。mProgressDialog.setIndeterminate(true);// 是否可以按回退键取消mProgressDialog.setCancelable(true);// 设置ProgressDialog的一个Button,可选三种值:BUTTON_POSITIVE,BUTTON_NEGATIVE,BUTTON_NEUTRALmProgressDialog.setButton(ProgressDialog.BUTTON_POSITIVE, “确定”, new DialogInterface.OnClickListener(){(DialogInterface dialog, int which){//点击确认之后的操作}});mProgressDialog.setButton(ProgressDialog.BUTTON_NEGATIVE, “取消”, new DialogInterface.OnClickListener(){(DialogInterface dialog, int which){//点击取消之后的操作}});mProgressDialog.show();

2.为了展示效果,添加异步任务AsyncTask模拟进度更新

AsyncTask<Void, Integer, Integer>{@Overrideprotected Integer doInBackground(Void… params){//模拟进度更新for (int i = 0; i <= 100; i++){try{Thread.sleep(40);publishProgress(i);}catch (InterruptedException e){e.printStackTrace();}}return 1;}@Overrideprotected void onProgressUpdate(Integer… values){if (values[0] == 100){mProgressDialog.dismiss();}else{mProgressDialog.setProgress(values[0]);}}@Overrideprotected void onPostExecute(Integer result){if (result == 1){Toast.makeText(MainActivity.this, “下载完成!”, Toast.LENGTH_SHORT).show();}}}

3.在mProgressDialog.show()方法后启动任务

// 启动AsyncTask异步任务更新Progress进度MyTask task = new MyTask();task.execute();

4.运行效果

二、透明Dialog

1.准备Dialog背景图片

2.在values/styles.xml中添加自定义透明风格

<style name=”MyDialogStyle”><item name=”android:windowBackground”>@android:color/transparent</item><item name=”android:windowFrame”>@null</item><!–边框–><item name=”android:windowNoTitle”>true</item><!–无标题–><item name=”android:windowIsFloating”>true</item><!–是否浮现在activity之上–><item name=”android:windowIsTranslucent”>true</item><!–半透明–> <item name=”android:windowContentOverlay”>@null</item><item name=”android:windowAnimationStyle”>@android:style/Animation.Dialog</item><item name=”android:backgroundDimEnabled”>true</item><!–模糊–> </style>

3.新建DialogActivity,修改主题为Dialog样式

<activityandroid:name=”.DialogActivity”android:label=”@string/title_activity_dialog”<!– 执行自己上面自定义的样式 –>android:theme=”@style/MyDialogStyle” ></activity>

4.添加布局文件activity_dialog.xml

=============></RelativeLayout> </RelativeLayout> 你写PPT时,阿拉斯加的鳕鱼正跃出水面,

【Android UI设计】Dialog对话框详解(二)

相关文章:

你感兴趣的文章:

标签云: