Java源码示例:androidx.core.widget.PopupWindowCompat

示例1
@Override
public void setWindowLayoutType(int type) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        super.setWindowLayoutType(type);
    } else {
        PopupWindowCompat.setWindowLayoutType(this, type);
    }
}
 
示例2
@Override
public int getWindowLayoutType() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        return super.getWindowLayoutType();
    } else {
        return PopupWindowCompat.getWindowLayoutType(this);
    }
}
 
示例3
@Override
public void setOverlapAnchor(boolean overlapAnchor) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        super.setOverlapAnchor(overlapAnchor);
    } else {
        PopupWindowCompat.setOverlapAnchor(this, overlapAnchor);
    }
}
 
示例4
/**
 * 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);
    final View contentView = getContentView();
    final Rect windowRect = new Rect();
    contentView.getWindowVisibleDisplayFrame(windowRect);
    final int windowW = windowRect.width();
    final int windowH = windowRect.height();
    contentView.measure(
            makeDropDownMeasureSpec(getWidth(), windowW),
            makeDropDownMeasureSpec(getHeight(), windowH)
    );
    final int measuredW = contentView.getMeasuredWidth();
    final int measuredH = contentView.getMeasuredHeight();
    final int[] anchorLocation = new int[2];
    anchor.getLocationInWindow(anchorLocation);
    final int anchorBottom = anchorLocation[1] + anchor.getHeight();
    if (!fitInScreen) {
        x += anchorLocation[0];
        y += anchorBottom;
    }
    switch (vertPos) {
        case VerticalPosition.ABOVE:
            y -= measuredH + anchor.getHeight();
            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:
            // Default position.
            break;
    }
    switch (horizPos) {
        case HorizontalPosition.LEFT:
            x -= measuredW;
            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();
            break;
    }
    if (fitInScreen) {
        if (y + anchorBottom < 0) {
            y = -anchorBottom;
        } else if (y + anchorBottom + measuredH > windowH) {
            y = windowH - anchorBottom - measuredH;
        }
        PopupWindowCompat.showAsDropDown(this, anchor, x, y, Gravity.NO_GRAVITY);
    } else {
        showAtLocation(anchor, Gravity.NO_GRAVITY, x, y);
    }
}
 
示例5
/**
 * 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();
    }
    switch (vertPos) {
        case VerticalPosition.ABOVE:
            y -= measuredH + anchor.getHeight();
            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:
            // Default position.
            break;
    }
    switch (horizPos) {
        case HorizontalPosition.LEFT:
            x -= measuredW;
            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();
            break;
    }
    if (fitInScreen) {
        PopupWindowCompat.showAsDropDown(this, anchor, x, y, Gravity.NO_GRAVITY);
    } else {
        showAtLocation(anchor, Gravity.NO_GRAVITY, x, y);
    }
}