功能:想要将pop显示到指定的view的位置周围,
思路:首先是获取到指定view的坐标点。然后让pop设置到指定view的周围

获取到指定的view坐标点
@Override
public void onWindowFocusChanged(boolean hasFocus) {
    super.onWindowFocusChanged(hasFocus);
    location = new int[2];
    tvSms1.getLocationInWindow(location); //获取在当前窗口内的绝对坐标

}

复习上面的方法然后通过getLocationInwindow();这个方法拿去到你指定view的位置tvSms1这个是我要获取的view的位置
location数组是将获取到的左边点写入到该对象中。location[0]表示的为x轴。location[1]表示的是Y轴,这时你已经获取到了指定view的坐标,下面就是显示,

popwindow的显示有:

public void showAtLocation(View parent, int gravity, int x, int y) {
    throw new RuntimeException("Stub!");
}

public void showAsDropDown(View anchor) {
    throw new RuntimeException("Stub!");
}

public void showAsDropDown(View anchor, int xoff, int yoff) {
    throw new RuntimeException("Stub!");
}

public void showAsDropDown(View anchor, int xoff, int yoff, int gravity) {
    throw new RuntimeException("Stub!");
}
对于pop的展示有上面4个方法其中3个是展示在指定view的下面。同时也可以设置对应的x.y的位置进行位置的变动。我使用的是通过showAtLocation这个方法进行弹出窗的显示代码如下

View popup_view = getLayoutInflater().inflate(R.layout.pop_copy, null);
popupWindow = new PopupWindow(popup_view, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
popupWindow.setBackgroundDrawable(new BitmapDrawable());
popupWindow.setOutsideTouchable(true);// 点击其他区域popupwindow消失,上面一句必不可少
popupWindow.showAtLocation(tvSms1, Gravity.LEFT | Gravity.TOP, location[0] - 30, location[1] - 20);
视图展示在tvSms1的左上。同时设置坐标点为tvSms1左边的移动点。location[0] - 30, location[1] - 20,location上面说了是指定view的坐标因此如果你需要显示到指定位置需要改动Gravity即可。它包含5个方法。上下左右和NO_GRAVITY根据自己的情况进行添加和组合。
下面是图片popWindow 显示指定的位置如果那个地方没有说到或者有误请告知谢谢观看

相关文章: