您的位置:首页 > 运维架构

在手指按下的位置弹出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坐标

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;
}
});

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: