Section1
popupWindow.showAsDropDown(tv_bottom_b); popupWindow.showAsDropDown(tv_bottom_b,100,100); popupWindow.showAsDropDown(tv_bottom_b,100,100, Gravity.RIGHT|Gravity.TOP);
第一个方法 showAsDropDown 我们前面已经用过了,不做分析
第二个方法多了两个参数,分别代表x和y方向的偏移量,单位是px,也比较简单
第三个方法需要API 19 的支持,多了个Gravity参数,也比较简单,
我们着重分析第四个方法
Section2
/** * <p> * Display the content view in a popup window at the specified location. If the popup window * cannot fit on screen, it will be clipped. See {@link android.view.WindowManager.LayoutParams} * for more information on how gravity and the x and y parameters are related. Specifying * a gravity of {@link android.view.Gravity#NO_GRAVITY} is similar to specifying * <code>Gravity.LEFT | Gravity.TOP</code>. * </p> * * @param parent a parent view to get the {@link android.view.View#getWindowToken()} token from * @param gravity the gravity which controls the placement of the popup window * @param x the popup's x location offset * @param y the popup's y location offset */ public void showAtLocation(View parent, int gravity, int x, int y) { showAtLocation(parent.getWindowToken(), gravity, x, y); }
通过实验我们发现,当使用showAtLocation方法的时候,
第一个参数view就不再影响popup window的位置了
这个时候要靠 gravity以及x,y 来确定view的位置。