Android使用Activity用作弹出式对话框Dialog
转载请表明出处: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用作弹出式对话框Dialog相关推荐
- Android之AlertDialog(弹出式对话框)的使用
一.简单的内容文本弹出式对话框 还是一样MainActivity的布局文件就不放上了,就是一个简单的Button控件,在Java代码中为其绑定了一个监听器. 首先我们需要创建这个AlertDialog ...
- 5弹出阴影遮罩_千文详述Cocos Creator弹出式对话框实现技术,着实硬核
正文 在Cocos Creator游戏开发中,经常需要使用到弹出式对话框,下面我们就一起来封装下自己的弹出式对话框. 一.弹出式对话框原理解析 1:对话框的结构: 1. `根节点 -->`2. ...
- android studio 弹出式对话框设置时间_如何设置当单击某个对象时运行指定的应用程序?...
在放映幻灯片的过程中,有时会需要启动其他应用程序,比如计算器或记事本来做一些辅助性的工作.在PowerPoint 2010中能否通过单击某个对象来运行指定的应用程序? 1解决方案 为指定对象设置动作效 ...
- Android 进入Activity禁止弹出软键盘输入法及stateHidden和stateAlwaysHidden的区别
在开发中经常会遇到这样的问题,进入一个含Edittext的Activity后总是会自动弹出软件盘.但是有时候我们又不想让它弹出,因为影响美观,而且用户可能也会反感.最好的做法是刚进入时不让弹出,在用户 ...
- Android studio Dialog 弹出式对话框
文件结构 这里写图片描述 运行效果 这里写图片描述 主要代码 MainActivity package cn.edu.sicnu.dialogdemo; import android.app.Dial ...
- java常用弹出式对话框
显示一个错误对话框,该对话框显示的 message 为 'alert': JOptionPane.showMessageDialog(null, "alert", "al ...
- Android编程:底部弹出输入框
Android编程:底部弹出的对话框 本文博客链接:http://blog.csdn.net/jdh99,作者:jdh,转载请注明. 环境: 主机:WIN10 开发环境:Android Studio ...
- 透明设置Android:将activity设置为弹出式的并设置为透明的
首先声明,我是一个菜鸟.一下文章中出现技术误导情况盖不负责 1. 在res/values 下建立color.xml <resources><colorname="trans ...
- Android自定义控件:NestedScrolling实现仿魅族flyme6应用市场应用详情弹出式layout
在前一篇博文中已经实现过一个仿魅族flyme6应用市场应用详情弹出式layout: Android自定义控件:从零开始实现魅族flyme6应用市场应用详情弹出式layout,主要是通过viewDrag ...
- android 7.1 权限自动授权(不弹出授权对话框)
1.第一种方式: 修改 DefaultPermissionGrantPolicy.java这个类 说明: 指定包名进行自动授权,即指定的包名进行动态授权时,不弹出授权对话框自动授权 源码路径:/sc6 ...
最新文章
- 量化人类社会交互行为
- c语言回文串判定代码_C语言编写一个程序,判断输入的一个字符串是否是回文。...
- php实现返回界面,PHP实现模仿socket请求返回页面的方法
- 写出高质量的代码——“零星”总结(延续3)
- jQuery LigerUI API预览版发布
- Caffe网络结构实现
- 细谈eclipse PDT开发WordPress插件
- STL之Ranges区间讨论
- 基于51单片机的交通灯控制设计
- pycharm专业版账号登录问题
- 12306火车票查询--python
- 不用安装Excel使用PSExcel自动处理Excel文件
- 用星号输出一个梯形,如下图所示:(使用嵌套for循环完成)
- 用R进行文本分析初探——以《红楼梦》为例
- linux 硬盘对拷,比ghost好用
- ML之prophet:利用prophet算法对维基百科页面的日志每日页面浏览量实现回归预测(时间序列的趋势/周季节性趋势/年季节性趋势)案例
- 手机看小说时如何设置上下或左右翻页模式
- 江苏移动千里眼 v2.1.12
- java并行任务,Java 并发编程学习(五):批量并行执行任务的两种方式
- 聊天机器人ChatGPT在医疗领域的应用场景
热门文章
- 加油中国,雄起汶川-快乐工作,快乐生活(多图)
- C语言简单游戏编程入门之四子棋
- Connect Four四子棋c++程序 - 显示窗口(0)
- 1331:【例1-2】后缀表达式的值
- [生存志] 第48节 叔向忧羊舌
- python unpack java,Java中的python struct.unpack - java
- LoadLibrary动态加载dll失败,GetLastError返回错误码126
- HGDB pg_stat_statements 用法
- linux ntfs格式u盘实现与挂载
- 为什么计算机三分技术七分管理,如何理解“七分管理,三分技术,运作贯穿始终”?...