Dialog && PopupWindow
来源:互联网 发布:学编程怎么开始学 编辑:程序博客网 时间:2024/05/29 15:48
项目中经常会遇到点击一个按钮,会弹出一个界面,在新的界面中可以有一些操作。例如,一个登陆的界面,弹出的一个界面中输入用户名和密码,然后点击登陆按钮就可以登陆了。这样弹出来的界面一般是Dialog或者是PopupWindow。但是Dialog和PopupWindow之间还是有一些异同的。
Dialog的基本使用。(注意不能使用getApplicationContext())
Dialog dialog = new Dialog(MainActivity.this); // 创建一个Dialog// 创建一个DialogImageView imageView = new ImageView(MainActivity.this);imageView.setBackgroundResource(R.drawable.ic_launcher);dialog.getWindow().setContentView(imageView); // 为Dialog设置一个View// dialog.setContentView(imageView);// 同上dialog.getWindow().setLayout(1080, 1920); // 设置Dialog的大小// 下面也是设置Dialog的大小// WindowManager.LayoutParams params = dialog.getWindow().getAttributes();// params.height = 1920;// params.width = 1080;// dialog.getWindow().setAttributes(params);dialog.show(); // 将Dialog展现出来
需要注意一点是,Dialog全屏的设置。实际上需要两步操作。
设置为没有Title同时是全屏的。
<style name="dialog"> <item name="android:windowNoTitle">true</item> <item name="android:windowFullscreen">true</item></style>设置全屏(这里不设置成match_parent好像不能全屏)
dialog.getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);或者
WindowManager.LayoutParams params = dialog.getWindow().getAttributes();params.height = ViewGroup.LayoutParams.MATCH_PARENT;params.width = ViewGroup.LayoutParams.MATCH_PARENT;dialog.getWindow().setAttributes(params);PopupWindow的基本使用。
PopupWindow popupWindow = new PopupWindow(getApplicationContext());popupWindow.setHeight(ViewGroup.LayoutParams.MATCH_PARENT);popupWindow.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);ImageView imageView = new ImageView(MainActivity.this);imageView.setBackgroundResource(R.drawable.ic_launcher);popupWindow.setContentView(imageView); // 将View放在Popupwindow中popupWindow.showAtLocation(v, Gravity.CENTER, 0, 0);// 设置Popup的位置,不设置不显示popupWindow.update();
Dialog与PopupWindow的区别
1.Dialog出来以后会拦截所有的事件而PopupWindow,也就是说你点击Dialog其他的地方是无法操作的,但是点击PopupWindow其他的大方是可以操作的。
2.PopupWindow是不能再onCreate()中创建的。
3.PopupWindow是可以设置相对位置的,也就是说可以将其设置在一个View的下面等等。showAsDropDown()
4.Dialog不能使用getApplicationContext()
最后贴一个PopupWindow点击返回键消失,点击外面不消失的方法。
public static void setPopupWindowTouchModal(PopupWindow popupWindow, boolean touchModal) { if (null == popupWindow) { return; } Method method; try { method = PopupWindow.class.getDeclaredMethod("setTouchModal", boolean.class); method.setAccessible(true); method.invoke(popupWindow, touchModal); } catch (Exception e) { e.printStackTrace(); }}通过反射将touchModal设置为false。最后需要加上。
setPopupWindowTouchModal(popupWindow, false);popupWindow.setFocusable(true);
0 0
- Dialog && PopupWindow
- Dialog中弹出PopupWindow
- Dialog&PopupWindow对话框
- Dialog和Popupwindow
- Toast,Dialog,PopupWindow,Notification
- Dialog、PopupWindow、Toast
- PopupWindow和Dialog
- Dialog中弹出PopupWindow
- Menu/Dialog/PopupWindow
- Dialog, DialogFragment, PopupWindow比较
- Toast,Dialog,PopupWindow,Notifaction
- android.widget.PopupWindow,生成Dialog
- 笔记41--Dialog和Popupwindow
- dialog和popupwindow和activityDialog
- 自定义Dialog、PopupWindow、通知Notification
- Dialog和Popupwindow的区别
- 使用PopupWindow完成各种dialog
- Android弹窗合集(PopupWindow,dialog)
- OpenCV中图像的读取,显示与输出
- logaction
- 二叉排序树
- 《java多线程编程核心技术》之java多线程技能
- Spring-Aop注解形式案例<二>
- Dialog && PopupWindow
- iOS之多线程
- JEECG 简单实例讲解自定义表单功能
- 如何让IE9以下版本认识html5元素
- 研究目标
- nova http 409 虚拟机状态重置
- 策略模式
- poj_1019
- 源码安装zabbix2.4