在控件的上下左右弹出popwidown
2015-11-24 21:41
639 查看
很简单的一个自定义的popwindown的基本类,主要功能是在任意布局的上下左右弹出pop对话框,如果不够用,自己修改代码在任意位置弹出pop窗口,也很简单。
<span style="font-size:18px;">public class BaseCustomPop extends PopupWindow {private View rootView;//pop的布局private int[] location = new int[2];private int margin = 10;//pop与控件的距离间隔public BaseCustomPop(View view) {this(view, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);}public BaseCustomPop(View view, int width, int height) {this(view, width, height, 0);}/*** @param layoutView* @param width* @param height* @param animationStyle*/public BaseCustomPop(View layoutView, int width, int height, int animationStyle) {// setAnimationStyle(R.style.pop_anim);this.setWidth(width);this.setHeight(height);this.rootView = layoutView;this.setAnimationStyle(animationStyle);this.setFocusable(true);this.setOutsideTouchable(true);this.setBackgroundDrawable(new BitmapDrawable());this.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);this.setContentView(layoutView);}public void show(View anchor) {show(anchor, POPGriVity.TOP);}public enum POPGriVity {TOP, BOTTOM, LEFT, RIGHT}public void setMargin(int margin) {this.margin = margin;}public void show(View anchor, POPGriVity grivity) {if (rootView == null)throw new IllegalStateException("setContentView was not called with a view to display.");if (anchor == null)throw new NullPointerException("anchor is null");int xPos, yPos;anchor.getLocationOnScreen(location);final Rect anchorRect = new Rect(location[0], location[1], location[0] + anchor.getWidth(),location[1] + anchor.getHeight());rootView.measure(0, 0);final int rootHeight = rootView.getMeasuredHeight();final int rootWidth = rootView.getMeasuredWidth();final int anchorCenterPosX = anchorRect.centerX();//默认显示在上面xPos = anchorCenterPosX - rootWidth / 2;yPos = anchorRect.top - rootHeight - margin;if (grivity == POPGriVity.TOP) {xPos = anchorCenterPosX - rootWidth / 2;yPos = anchorRect.top - rootHeight - margin;} else if (grivity == POPGriVity.BOTTOM) {xPos = anchorCenterPosX - rootWidth / 2;yPos = anchorRect.bottom + margin;} else if (grivity == POPGriVity.LEFT) {yPos = anchorRect.centerY()- rootHeight / 2;xPos = anchorRect.left - rootWidth - margin;} else if (grivity == POPGriVity.RIGHT) {yPos = anchorRect.centerY()- rootHeight / 2;xPos = anchorRect.right + margin;}showAtLocation(anchor, Gravity.NO_GRAVITY, xPos, yPos);}}</span>
相关文章推荐
- linux下利用系统函数实现rm -rf的功能!
- Tomcat
- Operating System-Process(2)进程表&&中断处理
- [zz]Linux系统相关shell命令
- $rootScope 与 $scope 区别:$rootScope对象是“单例的”——一个模块中只有一个$rootScope;
- Linux下Vsftp配置及详解
- Linus Bash Shell 的特殊变量和关系运算符
- WIN 7 64位环境中配置vs2015+opencv3.0
- iOS 系统架构及常用框架
- Linux进程冻结技术
- 使用 PopupWindow 自定义对话框遭遇的悲催事件和解决方法
- Linux/Cent OS 6.5 编译,安装obfuscator-llvm-3.6.1
- ubuntu12.04.5安装openssh-server所引发的血案
- Linux重定向:将标准输出stdout和标准错误stderr重定向到一个文件
- Hadoop学习笔记(一)Hadoop的单节点安装
- Hadoop学习笔记(一)Hadoop的单节点安装
- rhel-server-7.1更换yum源(CentOS7)
- Hadoop学习笔记(一)Hadoop的单节点安装
- Linux命令集锦
- LINK : fatal error LNK1168: cannot open Debug/CCounter.exe for writing 的解决办法