之前在做地图的时候,应用高德自带的infoWindow,无法满足业务需求,特做了一个自定义的popuWindow,现在来总结一下,上图
效果如上,这里封装了一个方法,直接调用就可以了
public void showPopupWindow(View view) {
// 这里呢,点击地图覆盖物,直接让其居中显示
aMap.animateCamera(CameraUpdateFactory.newCameraPosition(new CameraPosition(posiLnt, aMap.getCameraPosition().zoom, 0, 0)));
// 调用要展示的布局
View inflate = LayoutInflater.from(getActivity()).inflate(R.layout.pop_info, null);
// 设置popuwindow框中的内容
TextView xx= inflate.findViewById(R.id.xxx);
TextView xx= inflate.findViewById(R.id.xx);
// 第一个参数是用于PopupWindow中的View
// 第二个参数是PopupWindow的宽度,
// 第三个参数是PopupWindow的高度,
PopupWindow mPopupWindow = new PopupWindow(inflate, 500, 450);
// 这里可以设置popuWindow的背景
mPopupWindow.setBackgroundDrawable(new ColorDrawable());
// 设置PopupWindow是否能响应外部点击事件
mPopupWindow.setOutsideTouchable(true);
int[] position = new int[2];
view.getLocationOnScreen(position);
int y = position[1] + view.getMeasuredHeight();
// 第一个参数是PopupWindow的父View
// 第二个参数是PopupWindow相对父View的位置,
// 第三和第四个参数分别是PopupWindow相对父View的x、y偏移
mPopupWindow.showAtLocation(inflate, Gravity.RIGHT, 18, 28);
}
这里需要注意的地方,就是在fragment中调用这个弹框的时候,如果无法显示时,就在onCreateView中去判断全局view是否为null
if(view==null){
view = View.inflate(getActivity(), R.layout.fragment_home, null);
}
然后在调用的地方,再去判断view如果不为空的话,就去展示popuWindow
if (view != null) {
showPopupWindow(view);
}
关闭popuWindow的话,这个就不用说了,直接dismiss就可以。这里最好去判断一下popuWindow在不为空的状态下,再去关闭popuWindow