Android常用控件之PopupWindow详解
来源:互联网 发布:云控软件 编辑:程序博客网 时间:2024/04/28 02:35
定义
A popup window that can be used to display an arbitrary view. The popup window is a floating container that appears on top of the current activity.
PopupWindow即弹出框可以被用来显示任意的一个视图,它是一个悬浮在当前界面上部的容器。
构造方法
PopupWindow有很多构造方法,我们一般使用的是PopupWindow(View contentView, int width, int height, boolean focusable)方法来构造一个PopupWindow
常用方法
常用的方法有
可以通过这三个方法里将PopupWindow显示到对应于某一控件的任何位置。
showAsDropDown(View anchor)方法:将PopupWindow中的视图固定在anchor视图的左下角,默认偏移量为0.示例:
LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE); View popup = inflater.inflate(R.layout.popup_window, null); popupWindow = new PopupWindow(popup, LayoutParams.WRAP_CONTENT, 120, true); popupWindow.setOutsideTouchable(false); popupWindow.setBackgroundDrawable(new BitmapDrawable()); // popupWindow已目标控件的左下方为基准显示,默认偏移量为0; popupWindow.showAsDropDown(popupButton);
showAsDropDown(View anchor,int xoff,int yoff)方法:将PopupWindow中的视图固定在anchor视图的左下角,x,y偏移量分别为xoff,yoff.,偏移量均是相对于anchor视图的左下角定义的!示例:
LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE); View popup = inflater.inflate(R.layout.popup_window, null); popupWindow = new PopupWindow(popup, LayoutParams.WRAP_CONTENT, 120, true); popupWindow.setOutsideTouchable(false); popupWindow.setBackgroundDrawable(new BitmapDrawable()); // popupWindow已目标控件的左下方为基准显示,默认偏移量为0; // popupWindow.showAsDropDown(popupButton); // popupWindow已目标控件的左下方为基准显示,x偏移量为0,y偏移量为Button的高度与PopupWindow高度之和的负值,即PopupWindow显示在Button的上方 popupWindow.showAsDropDown(popupButton, 0, -(popupButton.getHeight() + popupWindow.getHeight()));
注意使用PopupWindow.getHeight()方法时,应确保PopupWindow的高度为一确定值,不能为WRAPCONTENT,否则该方法返回的是负值!
showAtLocation (View parent, int gravity, int x, int y) 方法: 将视图显示在特定的方向。x,y分别为PopupWindow的x,y偏移量,此偏移量是相对于整个屏幕来说的,gravity则定义了整个屏幕内的基准方向,Gravity.NO_GRAVITY相当于Gravity.LEFT|Gravtiy.TOP.
注意事项
1.PopupWindow点击外部区域消失的问题
如果想要实现点击外部区域消失的话必须要设置backgroundDrawable(),否则,无论设置OutsideTouchable()为true或false都将无效,点击外部区域或按下返回键都无任何反应。
如果设置了backgroundDrawable(),则论设置OutsideTouchable()为true或false,点击外部区域或按下返回键PopupWindow都将消失。So。。。Why??
- Android常用控件之PopupWindow详解
- android控件之popupWindow
- Android的PopupWindow控件详解
- android之PopupWindow详解一
- Android控件之PopupWindow弹出窗口
- Android应用之PopupWindow显示位置详解
- Android用户界面之常用控件ListView 详解加优化
- Android UI之在popupwindow中获取控件注意问题
- Android中的PopupWindow详解
- Android中的PopupWindow详解
- Android中的PopupWindow详解
- Android中的PopupWindow详解
- Android中的PopupWindow详解
- Android中的PopupWindow详解
- Android中的PopupWindow详解
- Android PopupWindow详解
- Android PopupWindow详解
- Android PopupWindow详解
- L3---fragment---ipsec---PMTU.discovery: PMTU discovery涉及到ipsec中的表现
- 矩阵的压缩存储
- APK签名原理
- 数据结构:请用一个数组实现两个堆栈,要求最大地利用数组空间,使 数组只要有空间入栈操作就可以成功
- 黑马程序员_日记34_JavaList特有方法
- Android常用控件之PopupWindow详解
- qt for linux (红帽) 终端编译
- IOS学习 CCCrypt加密和解密
- Sql server force select,delete with index
- atoi 的 C 语言实现
- jquery图表工具
- 7-1通信系统中为什么要进行编码和解码?常见的编码方法有哪些?
- python模块:xlsxwriter和xlrd相结合读取、写入excel文件
- Spring MVC 中的 forward 和 redirect