前言
首先谈谈将Activity作为Dialog初衷。
多数情况下,我们会直接选择使用AlertDialog和Dialog实现对话框,但是有以下原因驱使我们去寻找替代这种做法的方案。
原因一,调用dismiss方法,有时会出现异常:java.lang.IllegalArgumentException: View not attached to window manager。出现此异常原因可能是在dismiss对话框的时候,它所在的activity因为一些原因已经先destroy了。或者报另一种异常:Unable to add Window-token is null,这个问题是由于在Activity中显示Dialog时,当前Activity还未创建导致(主要是快速切换),因为Dialog是依附于当前Activity的某个View上的。产生这些问题的根本原因是dialog和activity有着不同的生命周期,多数情况下,不去控制,生命周期难以做到同步。
其二,官方不推荐直接使用Dialog创建对话框。google在android 3.0时新引入DialogFragment来替代Dialog,它是一种特殊的Fragment,用于在Activity的内容之上展示一个模态的对话框。典型的用于:展示警告框,输入框,确认框等等。使用DialogFragment来管理对话框,当旋转屏幕和按下后退键时可以更好的管理其生命周期,它和Fragment有着基本一致的生命周期。且DialogFragment也允许开发者把Dialog作为内嵌的组件进行重用,类似Fragment.(可以在大屏幕和小屏幕显示出不同的效果)
其三,基于以上两条,我们貌似可以结案了,本文也就没必要啰嗦下去了,既然DialogFragment可以满足需求,为什么还要使用Activity自定义Dialog呢?可能是我个人偏向原因,厌烦于Fragment复杂的生命周期和嵌套使用遇到的各种坑,当然这个理由在强大的Google推荐面前是站不住脚的,主要是项目中对基础数据的封装方式以及使用了MVP模式,使用Activity更容易展示数据以及与Presenter交互。
实现
整个实现过程并不复杂,只需要抓住以下几个关键点。
- 定义Activity主题themes.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="DialogActivity" parent="@style/AppTheme">
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item>
<item name="android:windowFrame">@null</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowIsFloating">true</item>
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowContentOverlay">@null</item>
<item name="android:backgroundDimEnabled">true</item>
</style>
</resources>
注意:一般情况下,parent=“android:style/Theme.Dialog”,如果你的Activity是继承AppCompatActivity的,则 parent=“@style/AppTheme”,否则会出现异常。
- 作为Dialog使用的Activity引用此主题
<activity android:name="com.xx.xxActivity"
android:theme="@style/DialogActivity"/>
- 在Activity的onCreate方法调用setContentView前加上
requestWindowFeature(Window.FEATURE_NO_TITLE);
2017.03.30–update
//在Activity中设置点击区域外消失属性
setFinishOnTouchOutside(true);
//dialog左上角关闭按钮,动画效果
- 布局
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="left|bottom"
android:layout_marginRight="25dp"
android:layout_marginTop="25dp"
android:background="@drawable/shape_round_rectangle_gray"
android:orientation="vertical"
android:gravity="center">
<!--dialog内容区域-->
</LinearLayout>
<!--关闭按钮-->
<ImageView
android:id="@+id/dialog_logout_close"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right|top"
android:contentDescription="@null"
android:src="@drawable/cr_icon_delete_black" />
</FrameLayout>
- 圆角对话框
//内容展示的父布局使用自定义圆角背景
android:background="@drawable/shape_round_rectangle_gray"
- 关闭按钮动画
//在initWidget()中开启动画
mDialogRotateAnim = AnimationUtils.loadAnimation(this, R.anim.rotating_once);
mCloseBtn.startAnimation(mDialogRotateAnim);
//rotating_once.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<!--转一圈-->
<rotate
android:duration="1000"
android:fromDegrees="360"
android:interpolator="@android:anim/accelerate_decelerate_interpolator"
android:pivotX="50%"
android:pivotY="50%"
android:startOffset="300"
android:toDegrees="0" />
</set>