* 简介:在项目开发时,经常会用到非模态对话框;虽然,可以用Popupwindow实现非模态对话框,但Popupwindow覆盖的部分不容易获取事件,且混合使用比较麻烦。 下面就用Android自身的Dialog来实现模态和非模态对话框,以及与界面绑定的对话框。*
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">
<RelativeLayout android:layout_width="140dp"
android:layout_height="140dp"
android:background="@drawable/wait_layer_back"
>
<RelativeLayout android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">
<FrameLayout android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:id="@+id/frame"
>
<ImageView android:layout_width="100"
android:layout_height="100"
android:background="@drawable/ym_progress"
android:id="@+id/progress"
/>
<ImageView android:layout_width="100"
android:layout_height="100"
android:background="@drawable/ym_progress_icon"
/>
FrameLayout>
<TextView android:id="@+id/tipText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="正在加载..."
android:textSize="16sp"
android:gravity="center"
android:textColor="#007E95"
android:layout_below="@+id/frame"
android:layout_marginTop="5dp"/>
RelativeLayout>
RelativeLayout>
LinearLayout>
<set
android:shareInterpolator="false"
xmlns:android="http://schemas.android.com/apk/res/android">
<rotate
android:interpolator="@android:anim/linear_interpolator"
android:pivotX="50%"
android:pivotY="50%"
android:fromDegrees="0"
android:toDegrees="+360"
android:duration="1000"
android:startOffset="-1"
android:repeatMode="restart"
android:repeatCount="-1" />
set>
4.样式
<resources>
<style name="loading_dialog" parent="android:style/Theme.Dialog">
<item name="android:windowFrame">@null
- "android:windowNoTitle"
>true
- "android:windowBackground"
>@android:color/transparent
- "android:windowIsFloating">true
- "android:windowContentOverlay">@null
style>
<style name="loading_dialog_dim" parent="loading_dialog">
<item name="android:backgroundDimEnabled">falseitem>
style>
resources>
5.主要代码
package com.single.oto.utils;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.util.Log;
import android.view.*;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.*;
import com.single.oto.R;
import com.single.oto.activity.BaseActivity;
public class WaitLayer {
private TextView tipTextView;
private Dialog loadingDialog;
private Animation hyperspaceJumpAnimation;
private ImageView spaceshipImage;
private DialogType dialogType = DialogType.MODALESS;
private ViewGroup rootView;
private View view;
public WaitLayer(Context context,DialogType dialogType) {
this.dialogType = dialogType;
creatDialog(context);
}
private void creatDialog(Context context) {
if(view == null){
view = LayoutInflater.from(context).inflate(R.layout.wait_layer,null);
}
hyperspaceJumpAnimation = AnimationUtils.loadAnimation(context,
R.anim.loading_animation);
// 使用ImageView显示动画
spaceshipImage = (ImageView)view.findViewById(R.id.progress);
tipTextView = (TextView)view.findViewById(R.id.tipText);
spaceshipImage.startAnimation(hyperspaceJumpAnimation);
if(dialogType == DialogType.NOT_NOMAL){//与界面绑定的对话框
if(context instanceof Activity){
rootView = (ViewGroup)((Activity)context).findViewById(android.R.id.content);
}else {
Log.e("waitLayer:","params context is not Activity");
}
}else {
if (dialogType == DialogType.MODAL){//模态
loadingDialog = new Dialog(context, R.style.loading_dialog_dim);// 创建自定义样式dialog
Window window = loadingDialog.getWindow();
WindowManager.LayoutParams wl = window.getAttributes();
wl.dimAmount = 0.0f;
// wl.alpha = 0f; //这句设置了对话框的透明度
wl.flags = wl.flags | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL;
wl.flags = wl.flags | WindowManager.LayoutParams.FLAG_DIM_BEHIND;
window.setAttributes(wl);
}else {//非模态
loadingDialog = new Dialog(context, R.style.loading_dialog);// 创建自定义样式dialog
loadingDialog.setCancelable(false);
}
loadingDialog.addContentView(view, new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT));
}
}
/**
* 显示等待层
*
*/
public void show() {
show(null);
}
/**
* 显示等待层
*
* @param msg
*/
public void show(String msg) {
if(dialogType == DialogType.NOT_NOMAL){
if(rootView.getChildAt(rootView.getChildCount() - 1) != view){
spaceshipImage.startAnimation(hyperspaceJumpAnimation);
rootView.addView(view, new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT));
}
}else {
if (loadingDialog.getWindow() != null
&& !loadingDialog.getWindow().isActive()) {
if (msg != null) {
tipTextView.setText(msg);// 设置加载信息,否则加载默认值
}else {
tipTextView.setText("");
}
if(!loadingDialog.isShowing()){
spaceshipImage.startAnimation(hyperspaceJumpAnimation);
loadingDialog.show();
}
}
}
}
/**
* 关闭等待层
*/
public void dismiss() {
if(dialogType != DialogType.NOT_NOMAL){
if (loadingDialog != null && loadingDialog.isShowing()) {
loadingDialog.dismiss();
}
}else {
if(rootView != null && view != null){
rootView.removeView(view);
}
}
hyperspaceJumpAnimation.cancel();
}
public enum DialogType{
MODAL,//模态
MODALESS,//非模态
NOT_NOMAL;//与界面绑定的对话框
}
}
小结:在创建对话框是根据enum DialogType的值来创建您所需要的对话框。