1. Dialog 与 AlertDialog 的区别。

  AlertDialog 是一种特殊形式的 Dialog。这个类中,我们可以添加一个,两个或者三个按钮,可以设置标题。所以,当我们想使用 AlertDialog 默认的按钮形式,用 AlertDialog 更加方便,而且有一个类 AlertDialog.Builder 很方便创建一个 AlertDialog。

  

2. Dialog 与 AlertDialog 写代码时需注意的事项。

  我们可以给一个 Dialog 用自定义的 Layout。有两个位置可以设置 Layout。

  • 一个是结构体中
  • 一个是 onCreate 方法中

  如果使用 AlertDialog,在结构体中需要写

        View contentView = LayoutInflater.from(context).inflate(R.layout.cutomized_dialog, null);
setView(contentView);

  而不能直接写 setContentView(R.layout...); 这会导致错误:E/AndroidRuntime(4544): android.util.AndroidRuntimeException: requestFeature() must be called before adding content
  
  在 onCreate 里写的话,可以直接用 setContentView(R.layout...); 而不会导致错误。但是,这两种方法是有区别的!

如果写在结构体里,用的是 LayoutInflater,而真正的外壳依然是默认的 Dialog 的一个外壳。也就是有 Dialog 默认的标题,默认的背景颜色等。

但如果写在 onCreate 里,使用 setContentView(R.layout...); 这种情形下,Dialog 默认的 Layout 外壳就不存在了,而使用我们自定义的 Layout。比如你的 Layout 的背景颜色是透明,那么出来的 Dialog 的背景就是透明的。而写在结构体里的情况则背景色是系统 Dialog 默认的(黑或白一般)。而且这种方法下不可以再使用系统默认的标题栏了,因为它已经不存在了。所以如果你写上:

        TextView titleText = (TextView) findViewById(android.R.id.title);
titleText.setText("testtitle");

你会得到一个 NullPointer 。因为 android.R.id.title 这个 Layout 已经被覆盖。

  如果使用 Dialog,在结构体里,可以直接写 setContentView(R.layout.cutomized_dialog); 而不用 LayoutInflater。

  在 onCreate 里的话,也是一样,直接写 setContentView(R.layout.cutomized_dialog);

  也就是说,如果用的是 Dialog,两种方法写没有任何区别。都会使用默认的 Dialog 样式。如果想修改样式,则需给 Dialog 提供自定义的样式。

我曾经需要创建一个背景透明的 Dialog,可是不管我怎么改 style,都没有成功。最后解决方案是使用了 AlertDialog 然后在 onCreate 方法中使用自己的 Layout,并把它的背景设置为透明。

Dialog 透明效果成功。解决方案是给 Dialog 的背景设置为一个透明的图片。(有一篇文章可供参考,http://blog.csdn.net/sodino/article/details/5822147

<item name="android:windowBackground">@drawable/a_transparent_image</item>

详情看下面的 xml 文件。

3. 一些参数

getWindow().setDimAmount(0);

  这个函数用来设置 Dialog 周围的颜色。系统默认的是半透明的灰色。值设为0则为完全透明。

<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="dialog" parent="@android:style/Theme.Dialog">
<!--边框-->
<item name="android:windowFrame">@null</item>
<!--是否浮现在activity之上-->
<item name="android:windowIsFloating">true</item>
<!--半透明-->
<item name="android:windowIsTranslucent">false</item>
<!--无标题-->
<item name="android:windowNoTitle">true</item>
<!--背景透明这种方法不好使-->
<item name="android:windowBackground">@color/transparent</item>
<!--背景透明正确方法-->
<item name="android:windowBackground">@drawable/a_transparent_image</item>
<!--模糊-->
<item name="android:backgroundDimEnabled">false</item>
</style>
</resources><item name="android:windowBackground">@color/transparent</item>

Android Dialog 的一些特性的更多相关文章

  1. Android 5.X新特性之为RecyclerView添加下拉刷新和上拉加载及SwipeRefreshLayout实现原理

    RecyclerView已经写过两篇文章了,分别是Android 5.X新特性之RecyclerView基本解析及无限复用 和 Android 5.X新特性之为RecyclerView添加Header ...

  2. Android 5.X新特性之为RecyclerView添加HeaderView和FooterView

    上一节我们讲到了 Android 5.X新特性之RecyclerView基本解析及无限复用 相信大家也应该熟悉了RecyclerView的基本使用,这一节我们来学习下,为RecyclerView添加H ...

  3. Android Dialog使用举例

    在Android开发中,我们经常会需要在Android界面上弹出一些对话框,比如询问用户或者让用户选择.这些功能我们叫它Android Dialog对话框,在我们使用Android的过程中,我归纳了一 ...

  4. 《Android群英传》读书笔记 (5) 第十一章 搭建云端服务器 + 第十二章 Android 5.X新特性详解 + 第十三章 Android实例提高

    第十一章 搭建云端服务器 该章主要介绍了移动后端服务的概念以及Bmob的使用,比较简单,所以略过不总结. 第十三章 Android实例提高 该章主要介绍了拼图游戏和2048的小项目实例,主要是代码,所 ...

  5. Android 6.0 新特性 整理 资料来自网络

    Android 6.0新特性 Runtime Permissions Doze and App Standby Apache HTTP Client Removal BoringSSL Access ...

  6. 腾讯云安全:开发者必看|Android 8.0 新特性及开发指南

    欢迎大家关注腾讯云技术社区-博客园官方主页,我们将持续在博客园为大家推荐技术精品文章哦~ 背景介绍 谷歌2017 I/O开发者大会今年将于5月17-19日在美国加州举办.大会将跟往年一样发布最新的 A ...

  7. Android 8.0 新特性

    Android 8.0 (Android Oreo(奥利奥))新特性介绍 通知渠道 - Notification Channels 通知渠道是由应用自行定义的通知内容类别,借助渠道,开发者可以让用户对 ...

  8. 开发者必看|Android 8.0 新特性及开发指南

    背景介绍 谷歌2017 I/O开发者大会今年将于5月17-19日在美国加州举办.大会将跟往年一样发布最新的 Android 系统,今年为 Android 8.0.谷歌在今年3 月21日发布 Andro ...

  9. android 7.0 新特性 和对开发者的影响

    android 7.0新特性 - jiabailong的专栏 - 博客频道 - CSDN.NEThttp://blog.csdn.net/jiabailong/article/details/5241 ...

随机推荐

  1. 【CentOS 6.5】 Qt Creator 启动失败

    在CentOS 6.5中 点击 [应用程序]->[编程]->Qt Creator , 没有反应,Creator没有启动,转而进入Shell cd /opt/Qt5.2.1/Tools/Qt ...

  2. 关于javascript中的typeof和instanceof介绍

    typeof用来检测给定变量的数据类型 instanceof用来检测对象的类型 typeof用来检测给定变量的数据类型(也可叫做基本类型,基本数据类型.包含undefined.boolean.stri ...

  3. 实战zabbix3.0.2 使用percona mysql插件监控mysql5.7

    1.系统环境 [root@shard0 templates]# cat /etc/redhat-release Red Hat Enterprise Linux Server release 7.2 ...

  4. Maven 快速构建一个项目

    参考:http://www.spring4all.com/article/266 mvn archetype:generate -DgroupId=springboot -DartifactId=sp ...

  5. JAVA_02

    class Test2_Extents{ public static void main(String[] args){ System.out.println("Hello World&qu ...

  6. 【转】C# 调用 C++ 数据转换

    原文:https://www.cnblogs.com/82767136/articles/2517457.html 在合作开发时,C#时常需要调用C++DLL,当传递参数时时常遇到问题,尤其是传递和返 ...

  7. 问题记录,StartCoroutine(“str")问题

    StartCoroutine参数为函数字符串名,运行时出错,错误是:无法启动协程函数. 调用格式如下: gameManager.StartCoroutine(LuaOnLevelwasloaded() ...

  8. 关于进程exit后,内存释放释放的实践

    最近碰到一个问题,或许也是小猿们都会碰到的问题:内存泄露. 都知道malloc后需要free才能释放内存,shmat后需要shmdt才能断掉内存区并使用IPC_RMID命令删除共享内存.那么如果是当前 ...

  9. js中获取页面元素节点的几种方式

    <!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...

  10. codeforces:855D Rowena Ravenclaw's Diadem分析和实现

    题目大意: 提供n个对象,分别编号为1,...,n.每个对象都可能是某个编号小于自己的对象的特例或是成分.认为某个对象的特例的特例依旧是该对象的特例,即特例关系传递,同样一个对象的成分的成分依旧是该对 ...