Android PopupWindow的基本使用
需求:希望点击文本区域的时候,在手指处添加一个控件,开始想到的是相对布局,添加一个隐藏层,根据点击然后显示并且重设坐标,后来发现这个方向是错的,甚至有点蠢,后来看到了popupWindow。
PopupWindow popupWindow = new PopupWindow(MainActivity.this);
//设置宽高
popupWindow.setWidth(300);
popupWindow.setHeight(150);
// 获取布局
popupWindow.setContentView(LayoutInflater.from(MainActivity.this).inflate(R.layout.dialog_item01, null));
// 点击区域外使对话框消失
popupWindow.setOutsideTouchable(false);
popupWindow.setFocusable(true);
popupWindow.setTouchable(true);
popupWindow.setBackgroundDrawable(new ColorDrawable(Color.WHITE));
// 获取控件位置
int[] lv2 = {0, 0};
tv.getLocationInWindow(lv2);
// 设置弹窗位置
popupWindow.showAtLocation(tv, Gravity.TOP | Gravity.LEFT, x+lv2[0], y+lv2[1]);以上方法初始化设置的固定宽高可能有问题,以后用下面这个
View contentView = LayoutInflater.from(MainActivity.this).inflate(R.layout.popuplayout, null); mPopWindow = new PopupWindow(contentView, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, true);

