注册

做一个透明的Dialog Activity

做一个透明的Dialog Activity

平时在很多软件中,肯定见到过从底部的弹窗,比如分享某个文件,从底部弹出的分享平台,大部分是通过PopupWindow 底部弹出实现,这次来讲一个不一样的。

1.png

1. 什么是 Dialog Activity

让Activity拥有和Dialog一样的效果,背景虚化,悬浮效果。

2. 为什么要使用 Dialog Activity

有时候我们需要在弹窗中去做复杂的逻辑,这就导致Dialog很臃肿,而拥有Dialog样式的Activity可以像我们写UI一样,使用架构去对复杂的逻辑进行层次划分,这样在逻辑上会清洗一些,在页面的声明周期上也更方便管理一些。

3. 怎么实现 Dialog Activity

3.1 写一个样式

<style name="Theme.ActivityDialogStyle" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:backgroundDimEnabled">true</item>
<item name="android:windowContentOverlay">@null</item>
<item name="android:windowCloseOnTouchOutside">false</item>
<item name="android:windowIsFloating">true</item>
</style>

其中的含义
windowIsTranslucent: 是否半透明
windowBackground: 设置dialog的背景
backgroundDimEnabled:背景是否模糊显示
windowContentOverlay:设置窗口内容不覆盖
windowCloseOnTouchOutside:
windowIsFloating:是否浮现在activity之上,这个属性很重要,设置为true之后,Activty的状态栏才会消失。

3.2 引用样式

 <activity
android:name=".MainActivity2"
android:theme="@style/Theme.ActivityDialogStyle" />

注意:activity必须要继承 AppCompatActivity。

3.3 可配置选项

如果需要设置圆角背景

在onCreate添加

getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));

如果想让该页面填满屏幕,大家知道Dialog默认是不填满的。 在onCreate添加

 Window window = getWindow();
// 把 DecorView 的默认 padding 取消,同时 DecorView 的默认大小也会取消
window.getDecorView().setPadding(0, 0, 0, 0);
WindowManager.LayoutParams layoutParams = window.getAttributes();
// 设置宽度
layoutParams.width = WindowManager.LayoutParams.MATCH_PARENT;
window.setAttributes(layoutParams);
// 给 DecorView 设置背景颜色,很重要,不然导致 Dialog 内容显示不全,有一部分内容会充当 padding,上面例子有举出
// window.getDecorView().setBackgroundColor(Color.GREEN);

3.4 踩坑

实际上整个的过程十分简单,但是诡异的事情发生了。我写了一个布局,是线性布局,它并没有什么问题,却一直无法正常显示。去掉 windowIsFloating 属性就好了,但windowIsFloating会造成dialog Activity存在状态栏。 最终通过修改布局解决,先看看第一个布局和效果。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity2">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />

<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="2"
android:background="@color/white" />

</LinearLayout>

可以看到这个Activity是显示了,因为背景有虚化,但layout_weight 为2的白色却没有显示。

2.png

修改后的布局(使用相对布局作为根布局):

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity2">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />

<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="2"
android:background="@color/white" />
</LinearLayout>
</RelativeLayout>

3.png

这样才正常显示,虽然解决了,但是原因真的好迷。

0 个评论

要回复文章请先登录注册