在手指按下的位置弹出PopWindow
2016-04-26 17:50
351 查看
先上效果图:
实现思路
1、使用PopupWindow.showAsDropDown(view,xoff,yoff) ,开始PopupWindow.showAsDropDown(view,0,0) 的话,如下图
关键是最后二个参数,获取当前手指按下的X轴和Y轴坐标,然后计算,使PopupWindow偏移到你想要的位置。
下面上代码:
1、
/**
* 初始化布局
*/
public void init() {
View pop_layout = LayoutInflater.from(mContext).inflate(R.layout.popwindow_msg_manage_layout, null);
// initView(pop_layout);
mPopupWindow = new PopupWindow(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
mPopupWindow.setContentView(pop_layout);
mPopupWindow.setFocusable(true);
mPopupWindow.setOutsideTouchable(true);
mPopupWindow.setBackgroundDrawable(new BitmapDrawable());
mPopupWindow.update();
int w = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
int h = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
pop_layout.measure(w, h);
//获取PopWindow宽和高
mHeight = pop_layout.getMeasuredHeight();
mWidth = pop_layout.getMeasuredWidth();
}
2、获取手指按下X和Y坐标
3、显示和计算偏移量
btn_window.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
//设置Pop显示偏移
int xoff = X - mWidth / 2;
int yoff = 0 - (v.getHeight() - Y) - mHeight;
mPopupWindow.showAsDropDown(v, xoff, yoff);
return true;
}
});
实现思路
1、使用PopupWindow.showAsDropDown(view,xoff,yoff) ,开始PopupWindow.showAsDropDown(view,0,0) 的话,如下图
关键是最后二个参数,获取当前手指按下的X轴和Y轴坐标,然后计算,使PopupWindow偏移到你想要的位置。
下面上代码:
1、
/**
* 初始化布局
*/
public void init() {
View pop_layout = LayoutInflater.from(mContext).inflate(R.layout.popwindow_msg_manage_layout, null);
// initView(pop_layout);
mPopupWindow = new PopupWindow(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
mPopupWindow.setContentView(pop_layout);
mPopupWindow.setFocusable(true);
mPopupWindow.setOutsideTouchable(true);
mPopupWindow.setBackgroundDrawable(new BitmapDrawable());
mPopupWindow.update();
int w = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
int h = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
pop_layout.measure(w, h);
//获取PopWindow宽和高
mHeight = pop_layout.getMeasuredHeight();
mWidth = pop_layout.getMeasuredWidth();
}
2、获取手指按下X和Y坐标
button.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { X = (int) event.getX(); Y = (int) event.getY(); return false; } });
3、显示和计算偏移量
btn_window.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
//设置Pop显示偏移
int xoff = X - mWidth / 2;
int yoff = 0 - (v.getHeight() - Y) - mHeight;
mPopupWindow.showAsDropDown(v, xoff, yoff);
return true;
}
});
相关文章推荐
- Linux shell脚本输出日志笔记整理
- Linux C程序设计的一些小技巧
- linux:指令与档案的搜索
- recyclerview点击item弹出popupwindow
- linux:问题
- Linux下chkconfig命令例子与参数详解
- Hadoop的概念、版本、发展史
- 下拉菜单(二) PopupWindow 封装实现
- Docker--私有仓库构建--RHEL7.2
- 安装配置nginx
- Mysql 数据库升级shell脚本编写与测试心得
- CentOS中查看物理CPU信息的方法
- linux C语言获取本机ip地址
- linux /etc/profile 对空格敏感。。。。
- xshell 中 alt+. 快捷键无法使用
- Apache+PHP开发环境的配置
- Looper & Handler
- linux软件包管理
- 前端推荐学习网站
- centos7 开启防火墙端口 firewalld