PopupWindow可以实现跟Dialog一样的效果,但相对于Dialog,PopupWindow可以设置任意的显示位置,其中有
1、showAtLocation()显示在指定位置,有两个方法重载:
public void showAtLocation(View parent,int gravity,int x,int y)
public void showAtLocation(View parent,int gravity,int x,int y)
这里我们可以根据具体的位置计算弹出窗口的位置
2、showAsDropDown()显示在一个参照物View的周围,有三个方法重载:
public void showAsDropDown(View anchor) 默认显示在参照物View的下方
public void showAsDropDown(View anchor, int xoff, int yoff)可以设置相应的偏移量显示
public void showAsDropDown(View anchor, int xoff, int yoff, int gravity)相对位置中设置gravity
PopupWindow设置外部点击消失必须设置
.setOutsideTouchable(true);
.setBackgroundDrawable(Drawable background);
二者缺一不可,否则没有效果。
代码如下:
View view = LayoutInflater.from(getActivity()).inflate(R.layout.warn_coach_dialog, null);
final PopupWindow popupWindow = new PopupWindow(view, LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.