@Override
  protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    // If we've been given an exact size to match, apply special formatting during layout.
    final boolean wasFormatted = mFormatItems;
    mFormatItems = MeasureSpec.getMode(widthMeasureSpec) == MeasureSpec.EXACTLY;

    if (wasFormatted != mFormatItems) {
      mFormatItemsWidth = 0; // Reset this when switching modes
    }

    // Special formatting can change whether items can fit as action buttons.
    // Kick the menu and update presenters when this changes.
    final int widthSize = MeasureSpec.getSize(widthMeasureSpec);
    if (mFormatItems && mMenu != null && widthSize != mFormatItemsWidth) {
      mFormatItemsWidth = widthSize;
      mMenu.onItemsChanged(true);
    }

    final int childCount = getChildCount();
    if (mFormatItems && childCount > 0) {
      onMeasureExactFormat(widthMeasureSpec, heightMeasureSpec);
    } else {
      // Previous measurement at exact format may have set margins - reset them.
      for (int i = 0; i < childCount; i++) {
        final View child = getChildAt(i);
        final LayoutParams lp = (LayoutParams) child.getLayoutParams();
        lp.leftMargin = lp.rightMargin = 0;
      }
      super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }
  }