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

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

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

例子:(消除了背景)

[html] view plaincopy
  1. <resources>
  2. <style name="dialog" parent="@android:style/Theme.Dialog">
  3. <item name="android:windowFrame">@null</item>
  4. <item name="android:windowIsFloating">true</item>
  5. <item name="android:windowIsTranslucent">false</item>
  6. <item name="android:windowNoTitle">true</item>
  7. <item name="android:background">@android:color/black</item>
  8. <item name="android:windowBackground">@null</item>
  9. <item name="android:backgroundDimEnabled">false</item>
  10. </style>
  11. </resources>
[java] view plaincopy
  1. Dialog dialog = new Dialog(SetActivity.this, R.style.dialog);
  2. dialog.setContentView(R.layout.test);
  3. dialog.show();

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

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

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

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

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

1、Activity的布局文件

[html] view plaincopy
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="wrap_content"
  4. android:layout_height="wrap_content"
  5. >
  6. <LinearLayout
  7. android:onClick="tip"
  8. android:layout_width="wrap_content"
  9. android:layout_height="wrap_content"
  10. android:layout_alignParentRight="true"
  11. android:layout_alignParentTop="true"
  12. android:layout_marginTop="46dp"
  13. android:background="@drawable/title_function_bg"
  14. android:orientation="vertical" >
  15. <TextView
  16. android:layout_width="fill_parent"
  17. android:layout_height="wrap_content"
  18. android:layout_marginTop="10dp"
  19. android:drawableLeft="@drawable/mm_title_btn_compose_normal"
  20. android:paddingLeft="10dp"
  21. android:paddingRight="10dp"
  22. android:text="发起聊天"
  23. android:textColor="#fff"
  24. android:textSize="20sp" />
  25. <TextView
  26. android:layout_width="fill_parent"
  27. android:layout_height="wrap_content"
  28. android:layout_marginTop="10dp"
  29. android:drawableLeft="@drawable/mm_title_btn_keyboard_normal"
  30. android:paddingLeft="10dp"
  31. android:paddingRight="10dp"
  32. android:text="登录网页版"
  33. android:textColor="#fff"
  34. android:textSize="20sp" />
  35. <TextView
  36. android:layout_width="fill_parent"
  37. android:layout_height="wrap_content"
  38. android:layout_marginTop="10dp"
  39. android:drawableLeft="@drawable/mm_title_btn_qrcode_normal"
  40. android:paddingLeft="10dp"
  41. android:paddingRight="10dp"
  42. android:text="扫一扫"
  43. android:textColor="#fff"
  44. android:textSize="20sp" />
  45. <TextView
  46. android:layout_width="fill_parent"
  47. android:layout_height="wrap_content"
  48. android:layout_marginBottom="10dp"
  49. android:layout_marginTop="10dp"
  50. android:drawableLeft="@drawable/mm_title_btn_receiver_normal"
  51. android:paddingLeft="10dp"
  52. android:paddingRight="10dp"
  53. android:text="听筒模式"
  54. android:textColor="#fff"
  55. android:textSize="20sp" />
  56. </LinearLayout>
  57. </RelativeLayout>

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

[html] view plaincopy
  1. <style name="MyDialogTopRight">
  2. <item name="android:windowBackground">@android:color/transparent</item>
  3. <item name="android:windowIsTranslucent">true</item>
  4. <item name="android:windowNoTitle">true</item>
  5. <item name="android:windowAnimationStyle">@style/Anim_scale</item>
  6. </style>

动画效果:

[html] view plaincopy
  1. <style name="Anim_scale" parent="@android:style/Animation.Activity">
  2. <item name="android:activityOpenEnterAnimation">@anim/scale_in</item>
  3. <item name="android:activityOpenExitAnimation">@anim/scale_out</item>
  4. <item name="android:activityCloseEnterAnimation">@anim/scale_in</item>
  5. <item name="android:activityCloseExitAnimation">@anim/scale_out</item>
  6. </style>

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

[java] view plaincopy
  1. Intent intent = new Intent(context, MainWeixinTitleRightActivity.class);
  2. startActivity(intent);

最终效果:

源码下载,点击这里

Android使用Activity用作弹出式对话框Dialog相关推荐

  1. Android之AlertDialog(弹出式对话框)的使用

    一.简单的内容文本弹出式对话框 还是一样MainActivity的布局文件就不放上了,就是一个简单的Button控件,在Java代码中为其绑定了一个监听器. 首先我们需要创建这个AlertDialog ...

  2. 5弹出阴影遮罩_千文详述Cocos Creator弹出式对话框实现技术,着实硬核

    正文 在Cocos Creator游戏开发中,经常需要使用到弹出式对话框,下面我们就一起来封装下自己的弹出式对话框. 一.弹出式对话框原理解析 1:对话框的结构: 1. `根节点 -->`2. ...

  3. android studio 弹出式对话框设置时间_如何设置当单击某个对象时运行指定的应用程序?...

    在放映幻灯片的过程中,有时会需要启动其他应用程序,比如计算器或记事本来做一些辅助性的工作.在PowerPoint 2010中能否通过单击某个对象来运行指定的应用程序? 1解决方案 为指定对象设置动作效 ...

  4. Android 进入Activity禁止弹出软键盘输入法及stateHidden和stateAlwaysHidden的区别

    在开发中经常会遇到这样的问题,进入一个含Edittext的Activity后总是会自动弹出软件盘.但是有时候我们又不想让它弹出,因为影响美观,而且用户可能也会反感.最好的做法是刚进入时不让弹出,在用户 ...

  5. Android studio Dialog 弹出式对话框

    文件结构 这里写图片描述 运行效果 这里写图片描述 主要代码 MainActivity package cn.edu.sicnu.dialogdemo; import android.app.Dial ...

  6. java常用弹出式对话框

    显示一个错误对话框,该对话框显示的 message 为 'alert': JOptionPane.showMessageDialog(null, "alert", "al ...

  7. Android编程:底部弹出输入框

    Android编程:底部弹出的对话框 本文博客链接:http://blog.csdn.net/jdh99,作者:jdh,转载请注明. 环境: 主机:WIN10 开发环境:Android Studio ...

  8. 透明设置Android:将activity设置为弹出式的并设置为透明的

    首先声明,我是一个菜鸟.一下文章中出现技术误导情况盖不负责 1. 在res/values 下建立color.xml <resources><colorname="trans ...

  9. Android自定义控件:NestedScrolling实现仿魅族flyme6应用市场应用详情弹出式layout

    在前一篇博文中已经实现过一个仿魅族flyme6应用市场应用详情弹出式layout: Android自定义控件:从零开始实现魅族flyme6应用市场应用详情弹出式layout,主要是通过viewDrag ...

  10. android 7.1 权限自动授权(不弹出授权对话框)

    1.第一种方式: 修改 DefaultPermissionGrantPolicy.java这个类 说明: 指定包名进行自动授权,即指定的包名进行动态授权时,不弹出授权对话框自动授权 源码路径:/sc6 ...

最新文章

  1. 量化人类社会交互行为
  2. c语言回文串判定代码_C语言编写一个程序,判断输入的一个字符串是否是回文。...
  3. php实现返回界面,PHP实现模仿socket请求返回页面的方法
  4. 写出高质量的代码——“零星”总结(延续3)
  5. jQuery LigerUI API预览版发布
  6. Caffe网络结构实现
  7. 细谈eclipse PDT开发WordPress插件
  8. STL之Ranges区间讨论
  9. 基于51单片机的交通灯控制设计
  10. pycharm专业版账号登录问题
  11. 12306火车票查询--python
  12. 不用安装Excel使用PSExcel自动处理Excel文件
  13. 用星号输出一个梯形,如下图所示:(使用嵌套for循环完成)
  14. 用R进行文本分析初探——以《红楼梦》为例
  15. linux 硬盘对拷,比ghost好用
  16. ML之prophet:利用prophet算法对维基百科页面的日志每日页面浏览量实现回归预测(时间序列的趋势/周季节性趋势/年季节性趋势)案例
  17. 手机看小说时如何设置上下或左右翻页模式
  18. 江苏移动千里眼 v2.1.12
  19. java并行任务,Java 并发编程学习(五):批量并行执行任务的两种方式
  20. 聊天机器人ChatGPT在医疗领域的应用场景

热门文章

  1. 加油中国,雄起汶川-快乐工作,快乐生活(多图)
  2. C语言简单游戏编程入门之四子棋
  3. Connect Four四子棋c++程序 - 显示窗口(0)
  4. 1331:【例1-2】后缀表达式的值
  5. [生存志] 第48节 叔向忧羊舌
  6. python unpack java,Java中的python struct.unpack - java
  7. LoadLibrary动态加载dll失败,GetLastError返回错误码126
  8. HGDB pg_stat_statements 用法
  9. linux ntfs格式u盘实现与挂载
  10. 为什么计算机三分技术七分管理,如何理解“七分管理,三分技术,运作贯穿始终”?...