转载请表明出处:http://blog.csdn.net/lmj623565791/article/details/23116115

Android中可用于实现对话框的有Dialog,PopupWindow,Activity。

下面简单介绍下,Dialog比较方便,但是显示位置比较固定,有时不能满足我们的需求。

例子:(消除了背景)

<resources>
<style name="dialog" parent="@android:style/Theme.Dialog">
<item name="android:windowFrame">@null</item>
<item name="android:windowIsFloating">true</item>
<item name="android:windowIsTranslucent">false</item>
<item name="android:windowNoTitle">true</item>
<item name="android:background">@android:color/black</item>
<item name="android:windowBackground">@null</item>
<item name="android:backgroundDimEnabled">false</item>
</style>
</resources>
 Dialog dialog = new Dialog(SetActivity.this, R.style.dialog);
dialog.setContentView(R.layout.test);
dialog.show();

PopupWindow可以通过showAtLocation设置显示位置,也可以通过ShowAsDropDown显示在某个View的相对位置,基本能满足我们的需求了。

下面介绍使用Activity作为弹出对话框,个人觉得好处有以下:

1、显示位置的设置,直接就是一个layout.xml随心所遇的感觉,可以实现在任何位置。

2、对对话框内控件的事件的处理,都独立出来在一个类中,调用的地方仅需要startActivity()就可以,这样可以使代码结构上更加清晰。

下面我们实现这个一个例子:

1、Activity的布局文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
> <LinearLayout
android:onClick="tip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginTop="46dp"
android:background="@drawable/title_function_bg"
android:orientation="vertical" > <TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:drawableLeft="@drawable/mm_title_btn_compose_normal"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:text="发起聊天"
android:textColor="#fff"
android:textSize="20sp" /> <TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:drawableLeft="@drawable/mm_title_btn_keyboard_normal"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:text="登录网页版"
android:textColor="#fff"
android:textSize="20sp" /> <TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:drawableLeft="@drawable/mm_title_btn_qrcode_normal"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:text="扫一扫"
android:textColor="#fff"
android:textSize="20sp" /> <TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_marginTop="10dp"
android:drawableLeft="@drawable/mm_title_btn_receiver_normal"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:text="听筒模式"
android:textColor="#fff"
android:textSize="20sp" />
</LinearLayout> </RelativeLayout>

2、在style.xml中定义一个theme(背景透明,无标题,动画效果),一般Activity默认动画效果右进右出,我们可以覆盖。

   <style name="MyDialogTopRight">
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowAnimationStyle">@style/Anim_scale</item>
</style>

动画效果:

    <style name="Anim_scale" parent="@android:style/Animation.Activity">
<item name="android:activityOpenEnterAnimation">@anim/scale_in</item>
<item name="android:activityOpenExitAnimation">@anim/scale_out</item>
<item name="android:activityCloseEnterAnimation">@anim/scale_in</item>
<item name="android:activityCloseExitAnimation">@anim/scale_out</item>
</style>

3、在需要使用的地方直接startActity()就可以了

Intent intent = new Intent(context, MainWeixinTitleRightActivity.class);
startActivity(intent);

最终效果:

源码下载,点击这里

Android使用Activity用作弹出式对话框的更多相关文章

  1. 【转】PyQt弹出式对话框的常用方法及标准按钮类型

    pyQt之弹出式对话框(QMessageBox)的常用方法及标准按钮类型 一.控件说明 QMessageBox是一种通用的弹出式对话框,用于显示消息,允许用户通过单击不同的标准按钮对消息进行反馈,且每 ...

  2. xamarin.android 实现 Activity 底部弹出对话框菜单

    Resources/drawable 下新增如下文件: push_bottom_in.xml <?xml version="1.0" encoding="utf-8 ...

  3. android弹出式菜单、弹出式对话框、弹出式窗口

    http://www.open-open.com/lib/view/open1389767042601.html http://www.open-open.com/lib/view/open13321 ...

  4. android 闹钟提醒并且在锁屏下弹出Dialog对话框并播放铃声和震动

    android 闹钟提醒并且在锁屏下弹出Dialog对话框并播放铃声和震动            1.先简单设置一个闹钟提醒事件: //设置闹钟 mSetting.setOnClickListener ...

  5. Android 如何解决dialog弹出时无法捕捉Activity的back事件

    Android 如何解决dialog弹出时无法捕捉Activity的back事件 在一些情况下,我们需要捕捉back键事件,然后在捕捉到的事件里写入我们需要进行的处理,通常可以采用下面三种办法捕捉到b ...

  6. 20150629_Andriod_06_插入_删除_弹出式操作数据

    Fr_06_view_s6 --> activity_f6_insert              --> activity_f7__delete ******************** ...

  7. web开发实战--弹出式富文本编辑器的实现思路和踩过的坑

    前言: 和弟弟合作, 一起整了个智慧屋的小web站点, 里面包含了很多经典的智力和推理题. 其实该站点从技术层面来分析的话, 也算一个信息发布站点. 因此在该网站的后台运营中, 富文本的编辑器显得尤为 ...

  8. PropertyGrid—为复杂属性提供下拉式编辑框和弹出式编辑框

    零.引言 PropertyGrid中我们经常看到一些下拉式的编辑方式(Color属性)和弹出式编辑框(字体),这些都是为一些复杂的属性提供的编辑方式,本文主要说明如何实现这样的编辑方式. 一.为属性提 ...

  9. 【Android】创建Popwindow弹出菜单的两种方式

    方法一的Activity package com.app.test02; import android.app.Activity; import android.os.Bundle; import a ...

随机推荐

  1. 齐博软件(地方门户系统) 文件加密破解工具

    原文:齐博软件(地方门户系统) 文件加密破解工具 本程序为针对"齐博软件地方门户系统5.0官方原版"的破解工具,一个垃圾系统居然弄出这么恶心的加密方式,有个鸟用!以后见一个破一个! ...

  2. HTML5系列之——applicationCache对象

    ApplicationCache主要简单介绍: applicationCache对象实现HTML5相应WEB离线功能.以下我们来主要解说applicationCache对象的一些主要功能和方法 app ...

  3. hdu3329(2次dfs)

    传送门:The Flood 题意:当水的高度升为多少的时候,能够将这块区域分为两个部分. 分析:枚举高度,先从外围开始一次dfs,将水能淹没的标记,然后看非标记的是否已分为多块. #include&l ...

  4. CentOS Kernel Source Install

    http://linuxmoz.com/centos-kernel-source-install/

  5. hello MemSQL 入门安装演示样例

    一,介绍 MemSQL号称世界上最快的分布式关系型数据库,兼容mysql但快30倍,能实现每秒150万次事务.原理是仅用内存并将SQL预编译为C++. 二,部署 官网下载地址:http://www.m ...

  6. hdu2102(bfs)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2102 分析:bfs求最短时间到达'P'点,不过本题有好几个trick,我都踩到了,自己还是太嫩了... ...

  7. Knockout应用开发指南 第四章:模板绑定

    原文:Knockout应用开发指南 第四章:模板绑定 模板绑定The template binding 目的 template绑定通过模板将数据render到页面.模板绑定对于构建嵌套结构的页面非常方 ...

  8. thinkphp框架的相关总结

    参考链接地址:http://gongwen.sinaapp.com/article-205.html 1. 模板中不能使用的标签 {$content} {$i} 2. If标签 如: <if c ...

  9. 《Nginx文件类型错误解析漏洞--攻击演练》 (转)

    今天看书看到其中提到的一个漏洞,那就是Nginx+PHP的服务器中,如果PHP的配置里 cgi.fix_pathinfo=1 那么就会产生一个漏洞.这个配置默认是1的,设为0会导致很多MVC框架(如T ...

  10. There is no Action mapped for namespace / and action name UserAction

    果断收藏了,说的非常具体.刚開始学习的人常常遇到的问题. There is no Action mapped for namespace / and action name UserAction 在网 ...