将popupwindow在某一个控件的指定位置显示,主要是用到showAtLocation、showAsDropDown方法。
popupwindow.showAtLocation(findViewById(R.id.layout), Gravity.NO_GRAVITY, 0 ,0 ) 第一个参数,是相对于哪个view来放置popupwindow 第二个参数,是grivity,这个用过LinearLayout就该知道是什么 最后两个参数是x,y方向的偏移量,x,y都可以正负值,当然对应了四个方向
指定控件上面显示:
int[] location = new int[2]; view.getLocationOnScreen(location); popupWindow.showAtLocation(view, Gravity.NO_GRAVITY, location[0], location[1]-popupWindow.getHeight());
指定控件下面显示:
popupWindow.showAsDropDown(view);
指定控件左面显示:
popupWindow.showAtLocation(view, Gravity.NO_GRAVITY, location[0]-popupWindow.getWidth(), location[1]);
指定控件右边面显示:
popupWindow.showAtLocation(v, Gravity.NO_GRAVITY, location[0]+v.getWidth(), location[1]);