下面列出了怎么用android.support.v4.widget.PopupWindowCompat的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* 相对anchor view显示,适用 宽高不为match_parent
* <p>
* 注意:如果使用 VerticalGravity 和 HorizontalGravity 时,请确保使用之后 PopupWindow 没有超出屏幕边界,
* 如果超出屏幕边界,VerticalGravity 和 HorizontalGravity 可能无效,从而达不到你想要的效果。
*
* @param anchor
* @param vertGravity 垂直方向的对齐方式
* @param horizGravity 水平方向的对齐方式
* @param x 水平方向的偏移
* @param y 垂直方向的偏移
*/
public void showAtAnchorView(@NonNull View anchor, @YGravity final int vertGravity, @XGravity int horizGravity, int x, int y) {
//防止忘记调用 apply() 方法
checkIsApply(true);
mAnchorView = anchor;
mOffsetX = x;
mOffsetY = y;
mYGravity = vertGravity;
mXGravity = horizGravity;
//处理背景变暗
handleBackgroundDim();
x = calculateX(anchor, horizGravity, mWidth, mOffsetX);
y = calculateY(anchor, vertGravity, mHeight, mOffsetY);
//是否重新获取宽高
if (isNeedReMeasureWH) {
registerOnGlobalLayoutListener();
}
// Log.i(TAG, "showAtAnchorView: w=" + measuredW + ",y=" + measuredH);
PopupWindowCompat.showAsDropDown(mPopupWindow, anchor, x, y, Gravity.NO_GRAVITY);
}
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
public void showAsDropDown(View anchor, int offsetX, int offsetY, int gravity) {
//防止忘记调用 apply() 方法
checkIsApply(false);
handleBackgroundDim();
mAnchorView = anchor;
mOffsetX = offsetX;
mOffsetY = offsetY;
//是否重新获取宽高
if (isNeedReMeasureWH) {
registerOnGlobalLayoutListener();
}
PopupWindowCompat.showAsDropDown(mPopupWindow, anchor, mOffsetX, mOffsetY, gravity);
}
@Override public void onClick(View v) {
if (clickListener != null) {
clickListener.onClick(v);
}
//using 8dip on axisX offset to make dropdown view visually be at start of dropdown itself
//using 4dip on axisY offset to make space between dropdown view and dropdown itself
//all offsets are necessary because of the dialog_holo_light_frame to display correctly on screen(shadow was made by inset)
int gravity;
int axisXOffset;
if (dropDownViewWidth + getX() > screenWidth) {
gravity = Gravity.TOP | Gravity.END;
axisXOffset = DimenUtils.dpToPixels(8);
}
else {
gravity = Gravity.TOP | Gravity.START;
axisXOffset = -DimenUtils.dpToPixels(8);
}
int axisYOffset = DimenUtils.dpToPixels(4);
switch (expandDirection) {
case UP:
PopupWindowCompat.showAsDropDown(dropdownWindow, v,
axisXOffset,
-dropDownViewHeight - getMeasuredHeight() - axisYOffset * 3,
gravity);
break;
case DOWN:
PopupWindowCompat.showAsDropDown(dropdownWindow, v,
axisXOffset,
-axisYOffset,
gravity);
break;
}
setSelected(true);
}
/**
* Show at relative position to anchor View with translation.
* @param anchor Anchor View
* @param vertPos Vertical Position Flag
* @param horizPos Horizontal Position Flag
* @param x Translation X
* @param y Translation Y
* @param fitInScreen Automatically fit in screen or not
*/
public void showOnAnchor(@NonNull View anchor, @VerticalPosition int vertPos, @HorizontalPosition int horizPos, int x, int y, boolean fitInScreen) {
setClippingEnabled(fitInScreen);
View contentView = getContentView();
contentView.measure(makeDropDownMeasureSpec(getWidth()), makeDropDownMeasureSpec(getHeight()));
final int measuredW = contentView.getMeasuredWidth();
final int measuredH = contentView.getMeasuredHeight();
if (!fitInScreen) {
final int[] anchorLocation = new int[2];
anchor.getLocationInWindow(anchorLocation);
x += anchorLocation[0];
y += anchorLocation[1] + anchor.getHeight();
}
ViewGroup.MarginLayoutParams anchorLP = (ViewGroup.MarginLayoutParams) anchor.getLayoutParams();
switch (vertPos) {
case VerticalPosition.ABOVE:
y -= measuredH + anchor.getHeight() + anchorLP.topMargin;
break;
case VerticalPosition.ALIGN_BOTTOM:
y -= measuredH;
break;
case VerticalPosition.CENTER:
y -= anchor.getHeight()/2 + measuredH/2;
break;
case VerticalPosition.ALIGN_TOP:
y -= anchor.getHeight();
break;
case VerticalPosition.BELOW:
y += anchorLP.topMargin;
break;
default:
break;
}
switch (horizPos) {
case HorizontalPosition.LEFT:
x -= measuredW + anchorLP.leftMargin;
break;
case HorizontalPosition.ALIGN_RIGHT:
x -= measuredW - anchor.getWidth();
break;
case HorizontalPosition.CENTER:
x += anchor.getWidth()/2 - measuredW/2;
break;
case HorizontalPosition.ALIGN_LEFT:
// Default position.
break;
case HorizontalPosition.RIGHT:
x += anchor.getWidth() + anchorLP.rightMargin;
break;
default:
break;
}
if (fitInScreen) {
PopupWindowCompat.showAsDropDown(this, anchor, x, y, Gravity.NO_GRAVITY);
} else {
showAtLocation(anchor, Gravity.NO_GRAVITY, x, y);
}
}