Beispiel #1
0
  private void setPopupOffset(
      ListPopupWindow popup, int screenRotation, Rect appRect, Rect padding) {
    int[] anchorLocation = new int[2];
    popup.getAnchorView().getLocationInWindow(anchorLocation);
    int anchorHeight = popup.getAnchorView().getHeight();

    // If we have a hardware menu button, locate the app menu closer to the estimated
    // hardware menu button location.
    if (mIsByPermanentButton) {
      int horizontalOffset = -anchorLocation[0];
      switch (screenRotation) {
        case Surface.ROTATION_0:
        case Surface.ROTATION_180:
          horizontalOffset += (appRect.width() - mPopup.getWidth()) / 2;
          break;
        case Surface.ROTATION_90:
          horizontalOffset += appRect.width() - mPopup.getWidth();
          break;
        case Surface.ROTATION_270:
          break;
        default:
          assert false;
          break;
      }
      popup.setHorizontalOffset(horizontalOffset);
      // The menu is displayed above the anchored view, so shift the menu up by the bottom
      // padding of the background.
      popup.setVerticalOffset(-padding.bottom);
    } else {
      // The menu is displayed over and below the anchored view, so shift the menu up by the
      // height of the anchor view.
      popup.setVerticalOffset(-mNegativeSoftwareVerticalOffset - anchorHeight);
    }
  }
Beispiel #2
0
  private void setMenuHeight(
      int numMenuItems, Rect appDimensions, int screenHeight, Rect padding, int footerHeight) {
    assert mPopup.getAnchorView() != null;
    View anchorView = mPopup.getAnchorView();
    int[] anchorViewLocation = new int[2];
    anchorView.getLocationOnScreen(anchorViewLocation);
    anchorViewLocation[1] -= appDimensions.top;
    int anchorViewImpactHeight = mIsByPermanentButton ? anchorView.getHeight() : 0;

    // Set appDimensions.height() for abnormal anchorViewLocation.
    if (anchorViewLocation[1] > screenHeight) {
      anchorViewLocation[1] = appDimensions.height();
    }
    int availableScreenSpace =
        Math.max(
            anchorViewLocation[1],
            appDimensions.height() - anchorViewLocation[1] - anchorViewImpactHeight);

    availableScreenSpace -= padding.bottom + footerHeight;
    if (mIsByPermanentButton) availableScreenSpace -= padding.top;

    int numCanFit = availableScreenSpace / (mItemRowHeight + mItemDividerHeight);

    // Fade out the last item if we cannot fit all items.
    if (numCanFit < numMenuItems) {
      int spaceForFullItems = numCanFit * (mItemRowHeight + mItemDividerHeight);
      int spaceForPartialItem = (int) (LAST_ITEM_SHOW_FRACTION * mItemRowHeight);
      // Determine which item needs hiding.
      if (spaceForFullItems + spaceForPartialItem < availableScreenSpace) {
        mPopup.setHeight(spaceForFullItems + spaceForPartialItem + padding.top + padding.bottom);
      } else {
        mPopup.setHeight(
            spaceForFullItems
                - mItemRowHeight
                + spaceForPartialItem
                + padding.top
                + padding.bottom);
      }
    } else {
      mPopup.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
    }
  }