/// <summary> /// Helper for makeAndAddView to set the position of a view /// and fill out its layout paramters. /// </summary> /// <remarks> /// Helper for makeAndAddView to set the position of a view /// and fill out its layout paramters. /// </remarks> /// <param name="child">The view to position</param> private void setUpChild(android.view.View child) { // Respect layout params that are already in the view. Otherwise // make some up... android.view.ViewGroup.LayoutParams lp = child.getLayoutParams(); if (lp == null) { lp = generateDefaultLayoutParams(); } addViewInLayout(child, 0, lp); child.setSelected(hasFocus()); // Get measure specs int childHeightSpec = android.view.ViewGroup.getChildMeasureSpec(mHeightMeasureSpec , mSpinnerPadding.top + mSpinnerPadding.bottom, lp.height); int childWidthSpec = android.view.ViewGroup.getChildMeasureSpec(mWidthMeasureSpec , mSpinnerPadding.left + mSpinnerPadding.right, lp.width); // Measure child child.measure(childWidthSpec, childHeightSpec); int childLeft; int childRight; // Position vertically based on gravity setting int childTop = mSpinnerPadding.top + ((getMeasuredHeight() - mSpinnerPadding.bottom - mSpinnerPadding.top - child.getMeasuredHeight()) / 2); int childBottom = childTop + child.getMeasuredHeight(); int width = child.getMeasuredWidth(); childLeft = 0; childRight = childLeft + width; child.layout(childLeft, childTop, childRight, childBottom); }
protected internal virtual int positionChildInverse(android.view.View child, int x, int y, int contentHeight) { int childWidth = child.getMeasuredWidth(); int childHeight = child.getMeasuredHeight(); int childTop = y + (contentHeight - childHeight) / 2; child.layout(x - childWidth, childTop, x, childTop + childHeight); return(childWidth); }
protected internal override void onMeasure(int widthMeasureSpec, int heightMeasureSpec ) { android.view.View child = mActivityChooserContent; // If the default action is not visible we want to be as tall as the // ActionBar so if this widget is used in the latter it will look as // a normal action button. if (mDefaultActivityButton.getVisibility() != VISIBLE) { heightMeasureSpec = android.view.View.MeasureSpec.makeMeasureSpec(android.view.View .MeasureSpec.getSize(heightMeasureSpec), android.view.View.MeasureSpec.EXACTLY); } measureChild(child, widthMeasureSpec, heightMeasureSpec); setMeasuredDimension(child.getMeasuredWidth(), child.getMeasuredHeight()); }
protected internal override void onLayout(bool changed, int l, int t, int r, int b) { int count = getChildCount(); { for (int i = 0; i < count; i++) { android.view.View child = getChildAt(i); if (child.getVisibility() != GONE) { android.widget.AbsoluteLayout.LayoutParams lp = (android.widget.AbsoluteLayout.LayoutParams )child.getLayoutParams(); int childLeft = mPaddingLeft + lp.x; int childTop = mPaddingTop + lp.y; child.layout(childLeft, childTop, childLeft + child.getMeasuredWidth(), childTop + child.getMeasuredHeight()); } } } }
protected internal override void onMeasure(int widthMeasureSpec, int heightMeasureSpec ) { int count = getChildCount(); int maxHeight = 0; int maxWidth = 0; // Find out how big everyone wants to be measureChildren(widthMeasureSpec, heightMeasureSpec); { // Find rightmost and bottom-most child for (int i = 0; i < count; i++) { android.view.View child = getChildAt(i); if (child.getVisibility() != GONE) { int childRight; int childBottom; android.widget.AbsoluteLayout.LayoutParams lp = (android.widget.AbsoluteLayout.LayoutParams )child.getLayoutParams(); childRight = lp.x + child.getMeasuredWidth(); childBottom = lp.y + child.getMeasuredHeight(); maxWidth = System.Math.Max(maxWidth, childRight); maxHeight = System.Math.Max(maxHeight, childBottom); } } } // Account for padding too maxWidth += mPaddingLeft + mPaddingRight; maxHeight += mPaddingTop + mPaddingBottom; // Check against minimum height and width maxHeight = System.Math.Max(maxHeight, getSuggestedMinimumHeight()); maxWidth = System.Math.Max(maxWidth, getSuggestedMinimumWidth()); setMeasuredDimension(resolveSizeAndState(maxWidth, widthMeasureSpec, 0), resolveSizeAndState (maxHeight, heightMeasureSpec, 0)); }
protected internal override void onMeasure(int widthMeasureSpec, int heightMeasureSpec ) { base.onMeasure(widthMeasureSpec, heightMeasureSpec); if (mActionBarView == null) { return; } android.widget.FrameLayout.LayoutParams lp = (android.widget.FrameLayout.LayoutParams )mActionBarView.getLayoutParams(); int actionBarViewHeight = mActionBarView.isCollapsed() ? 0 : mActionBarView.getMeasuredHeight () + lp.topMargin + lp.bottomMargin; if (mTabContainer != null && mTabContainer.getVisibility() != GONE) { int mode = android.view.View.MeasureSpec.getMode(heightMeasureSpec); if (mode == android.view.View.MeasureSpec.AT_MOST) { int maxHeight = android.view.View.MeasureSpec.getSize(heightMeasureSpec); setMeasuredDimension(getMeasuredWidth(), System.Math.Min(actionBarViewHeight + mTabContainer .getMeasuredHeight(), maxHeight)); } } }
protected internal override void onLayout(bool changed, int left, int top, int right , int bottom) { if (!mFormatItems) { base.onLayout(changed, left, top, right, bottom); return; } int childCount = getChildCount(); int midVertical = (top + bottom) / 2; int dividerWidth = getDividerWidth(); int overflowWidth = 0; int nonOverflowWidth = 0; int nonOverflowCount = 0; int widthRemaining = right - left - getPaddingRight() - getPaddingLeft(); bool hasOverflow = false; { for (int i = 0; i < childCount; i++) { android.view.View v = getChildAt(i); if (v.getVisibility() == GONE) { continue; } [email protected] p = ([email protected] .LayoutParams)v.getLayoutParams(); if (p.isOverflowButton) { overflowWidth = v.getMeasuredWidth(); if (hasDividerBeforeChildAt(i)) { overflowWidth += dividerWidth; } int height = v.getMeasuredHeight(); int r = getWidth() - getPaddingRight() - p.rightMargin; int l = r - overflowWidth; int t = midVertical - (height / 2); int b = t + height; v.layout(l, t, r, b); widthRemaining -= overflowWidth; hasOverflow = true; } else { int size = v.getMeasuredWidth() + p.leftMargin + p.rightMargin; nonOverflowWidth += size; widthRemaining -= size; if (hasDividerBeforeChildAt(i)) { nonOverflowWidth += dividerWidth; } nonOverflowCount++; } } } if (childCount == 1 && !hasOverflow) { // Center a single child android.view.View v = getChildAt(0); int width = v.getMeasuredWidth(); int height = v.getMeasuredHeight(); int midHorizontal = (right - left) / 2; int l = midHorizontal - width / 2; int t = midVertical - height / 2; v.layout(l, t, l + width, t + height); return; } int spacerCount = nonOverflowCount - (hasOverflow ? 0 : 1); int spacerSize = System.Math.Max(0, spacerCount > 0 ? widthRemaining / spacerCount : 0); int startLeft = getPaddingLeft(); { for (int i_1 = 0; i_1 < childCount; i_1++) { android.view.View v = getChildAt(i_1); [email protected] lp = ([email protected] .LayoutParams)v.getLayoutParams(); if (v.getVisibility() == GONE || lp.isOverflowButton) { continue; } startLeft += lp.leftMargin; int width = v.getMeasuredWidth(); int height = v.getMeasuredHeight(); int t = midVertical - height / 2; v.layout(startLeft, t, startLeft + width, t + height); startLeft += width + lp.rightMargin + spacerSize; } } }
private void onMeasureExactFormat(int widthMeasureSpec, int heightMeasureSpec) { // We already know the width mode is EXACTLY if we're here. int heightMode = android.view.View.MeasureSpec.getMode(heightMeasureSpec); int widthSize = android.view.View.MeasureSpec.getSize(widthMeasureSpec); int heightSize = android.view.View.MeasureSpec.getSize(heightMeasureSpec); int widthPadding = getPaddingLeft() + getPaddingRight(); int heightPadding = getPaddingTop() + getPaddingBottom(); widthSize -= widthPadding; // Divide the view into cells. int cellCount = widthSize / mMinCellSize; int cellSizeRemaining = widthSize % mMinCellSize; if (cellCount == 0) { // Give up, nothing fits. setMeasuredDimension(widthSize, 0); return; } int cellSize = mMinCellSize + cellSizeRemaining / cellCount; int cellsRemaining = cellCount; int maxChildHeight = 0; int maxCellsUsed = 0; int expandableItemCount = 0; int visibleItemCount = 0; bool hasOverflow = false; // This is used as a bitfield to locate the smallest items present. Assumes childCount < 64. long smallestItemsAt = 0; int childCount = getChildCount(); { for (int i = 0; i < childCount; i++) { android.view.View child = getChildAt(i); if (child.getVisibility() == GONE) { continue; } bool isGeneratedItem = child is [email protected]; visibleItemCount++; if (isGeneratedItem) { // Reset padding for generated menu item views; it may change below // and views are recycled. child.setPadding(mGeneratedItemPadding, 0, mGeneratedItemPadding, 0); } [email protected] lp = ([email protected] .LayoutParams)child.getLayoutParams(); lp.expanded = false; lp.extraPixels = 0; lp.cellsUsed = 0; lp.expandable = false; lp.leftMargin = 0; lp.rightMargin = 0; lp.preventEdgeOffset = isGeneratedItem && (([email protected] )child).hasText(); // Overflow always gets 1 cell. No more, no less. int cellsAvailable = lp.isOverflowButton ? 1 : cellsRemaining; int cellsUsed = measureChildForCells(child, cellSize, cellsAvailable, heightMeasureSpec , heightPadding); maxCellsUsed = System.Math.Max(maxCellsUsed, cellsUsed); if (lp.expandable) { expandableItemCount++; } if (lp.isOverflowButton) { hasOverflow = true; } cellsRemaining -= cellsUsed; maxChildHeight = System.Math.Max(maxChildHeight, child.getMeasuredHeight()); if (cellsUsed == 1) { smallestItemsAt |= (1 << i); } } } // When we have overflow and a single expanded (text) item, we want to try centering it // visually in the available space even though overflow consumes some of it. bool centerSingleExpandedItem = hasOverflow && visibleItemCount == 2; // Divide space for remaining cells if we have items that can expand. // Try distributing whole leftover cells to smaller items first. bool needsExpansion = false; while (expandableItemCount > 0 && cellsRemaining > 0) { int minCells = int.MaxValue; long minCellsAt = 0; // Bit locations are indices of relevant child views int minCellsItemCount = 0; { for (int i_1 = 0; i_1 < childCount; i_1++) { android.view.View child = getChildAt(i_1); [email protected] lp = ([email protected] .LayoutParams)child.getLayoutParams(); // Don't try to expand items that shouldn't. if (!lp.expandable) { continue; } // Mark indices of children that can receive an extra cell. if (lp.cellsUsed < minCells) { minCells = lp.cellsUsed; minCellsAt = 1 << i_1; minCellsItemCount = 1; } else { if (lp.cellsUsed == minCells) { minCellsAt |= 1 << i_1; minCellsItemCount++; } } } } // Items that get expanded will always be in the set of smallest items when we're done. smallestItemsAt |= minCellsAt; if (minCellsItemCount > cellsRemaining) { break; } // Couldn't expand anything evenly. Stop. // We have enough cells, all minimum size items will be incremented. minCells++; { for (int i_2 = 0; i_2 < childCount; i_2++) { android.view.View child = getChildAt(i_2); [email protected] lp = ([email protected] .LayoutParams)child.getLayoutParams(); if ((minCellsAt & (1 << i_2)) == 0) { // If this item is already at our small item count, mark it for later. if (lp.cellsUsed == minCells) { smallestItemsAt |= 1 << i_2; } continue; } if (centerSingleExpandedItem && lp.preventEdgeOffset && cellsRemaining == 1) { // Add padding to this item such that it centers. child.setPadding(mGeneratedItemPadding + cellSize, 0, mGeneratedItemPadding, 0); } lp.cellsUsed++; lp.expanded = true; cellsRemaining--; } } needsExpansion = true; } // Divide any space left that wouldn't divide along cell boundaries // evenly among the smallest items bool singleItem = !hasOverflow && visibleItemCount == 1; if (cellsRemaining > 0 && smallestItemsAt != 0 && (cellsRemaining < visibleItemCount - 1 || singleItem || maxCellsUsed > 1)) { float expandCount = Sharpen.Util.Long_GetBitCount(smallestItemsAt); if (!singleItem) { // The items at the far edges may only expand by half in order to pin to either side. if ((smallestItemsAt & 1) != 0) { [email protected] lp = ([email protected] .LayoutParams)getChildAt(0).getLayoutParams(); if (!lp.preventEdgeOffset) { expandCount -= 0.5f; } } if ((smallestItemsAt & (1 << (childCount - 1))) != 0) { [email protected] lp = (([email protected] .LayoutParams)getChildAt(childCount - 1).getLayoutParams()); if (!lp.preventEdgeOffset) { expandCount -= 0.5f; } } } int extraPixels = expandCount > 0 ? (int)(cellsRemaining * cellSize / expandCount ) : 0; { for (int i_1 = 0; i_1 < childCount; i_1++) { if ((smallestItemsAt & (1 << i_1)) == 0) { continue; } android.view.View child = getChildAt(i_1); [email protected] lp = ([email protected] .LayoutParams)child.getLayoutParams(); if (child is [email protected]) { // If this is one of our views, expand and measure at the larger size. lp.extraPixels = extraPixels; lp.expanded = true; if (i_1 == 0 && !lp.preventEdgeOffset) { // First item gets part of its new padding pushed out of sight. // The last item will get this implicitly from layout. lp.leftMargin = -extraPixels / 2; } needsExpansion = true; } else { if (lp.isOverflowButton) { lp.extraPixels = extraPixels; lp.expanded = true; lp.rightMargin = -extraPixels / 2; needsExpansion = true; } else { // If we don't know what it is, give it some margins instead // and let it center within its space. We still want to pin // against the edges. if (i_1 != 0) { lp.leftMargin = extraPixels / 2; } if (i_1 != childCount - 1) { lp.rightMargin = extraPixels / 2; } } } } } cellsRemaining = 0; } // Remeasure any items that have had extra space allocated to them. if (needsExpansion) { int heightSpec = android.view.View.MeasureSpec.makeMeasureSpec(heightSize - heightPadding , heightMode); { for (int i_1 = 0; i_1 < childCount; i_1++) { android.view.View child = getChildAt(i_1); [email protected] lp = ([email protected] .LayoutParams)child.getLayoutParams(); if (!lp.expanded) { continue; } int width = lp.cellsUsed * cellSize + lp.extraPixels; child.measure(android.view.View.MeasureSpec.makeMeasureSpec(width, android.view.View .MeasureSpec.EXACTLY), heightSpec); } } } if (heightMode != android.view.View.MeasureSpec.EXACTLY) { heightSize = maxChildHeight; } setMeasuredDimension(widthSize, heightSize); mMeasuredExtraWidth = cellsRemaining * cellSize; }
protected internal override void onMeasure(int widthMeasureSpec, int heightMeasureSpec ) { int widthMode = android.view.View.MeasureSpec.getMode(widthMeasureSpec); if (widthMode != android.view.View.MeasureSpec.EXACTLY) { throw new System.InvalidOperationException(GetType().Name + " can only be used " + "with android:layout_width=\"match_parent\" (or fill_parent)"); } int heightMode = android.view.View.MeasureSpec.getMode(heightMeasureSpec); if (heightMode == android.view.View.MeasureSpec.UNSPECIFIED) { throw new System.InvalidOperationException(GetType().Name + " can only be used " + "with android:layout_height=\"wrap_content\""); } int contentWidth = android.view.View.MeasureSpec.getSize(widthMeasureSpec); int maxHeight = mContentHeight > 0 ? mContentHeight : android.view.View.MeasureSpec .getSize(heightMeasureSpec); int verticalPadding = getPaddingTop() + getPaddingBottom(); int availableWidth = contentWidth - getPaddingLeft() - getPaddingRight(); int height = maxHeight - verticalPadding; int childSpecHeight = android.view.View.MeasureSpec.makeMeasureSpec(height, android.view.View .MeasureSpec.AT_MOST); if (mClose != null) { availableWidth = measureChildView(mClose, availableWidth, childSpecHeight, 0); android.view.ViewGroup.MarginLayoutParams lp = (android.view.ViewGroup.MarginLayoutParams )mClose.getLayoutParams(); availableWidth -= lp.leftMargin + lp.rightMargin; } if (mMenuView != null && mMenuView.getParent() == this) { availableWidth = measureChildView(mMenuView, availableWidth, childSpecHeight, 0); } if (mTitleLayout != null && mCustomView == null) { availableWidth = measureChildView(mTitleLayout, availableWidth, childSpecHeight, 0); } if (mCustomView != null) { android.view.ViewGroup.LayoutParams lp = mCustomView.getLayoutParams(); int customWidthMode = lp.width != android.view.ViewGroup.LayoutParams.WRAP_CONTENT ? android.view.View.MeasureSpec.EXACTLY : android.view.View.MeasureSpec.AT_MOST; int customWidth = lp.width >= 0 ? System.Math.Min(lp.width, availableWidth) : availableWidth; int customHeightMode = lp.height != android.view.ViewGroup.LayoutParams.WRAP_CONTENT ? android.view.View.MeasureSpec.EXACTLY : android.view.View.MeasureSpec.AT_MOST; int customHeight = lp.height >= 0 ? System.Math.Min(lp.height, height) : height; mCustomView.measure(android.view.View.MeasureSpec.makeMeasureSpec(customWidth, customWidthMode ), android.view.View.MeasureSpec.makeMeasureSpec(customHeight, customHeightMode) ); } if (mContentHeight <= 0) { int measuredHeight = 0; int count = getChildCount(); { for (int i = 0; i < count; i++) { android.view.View v = getChildAt(i); int paddedViewHeight = v.getMeasuredHeight() + verticalPadding; if (paddedViewHeight > measuredHeight) { measuredHeight = paddedViewHeight; } } } setMeasuredDimension(contentWidth, measuredHeight); } else { setMeasuredDimension(contentWidth, maxHeight); } }
internal virtual int getChildHeight(android.view.View child) { return(child.getMeasuredHeight()); }
protected internal override void onLayout(bool changed, int left, int top, int right , int bottom) { int count = getChildCount(); int parentLeft = getPaddingLeftWithForeground(); int parentRight = right - left - getPaddingRightWithForeground(); int parentTop = getPaddingTopWithForeground(); int parentBottom = bottom - top - getPaddingBottomWithForeground(); mForegroundBoundsChanged = true; { for (int i = 0; i < count; i++) { android.view.View child = getChildAt(i); if (child.getVisibility() != GONE) { android.widget.FrameLayout.LayoutParams lp = (android.widget.FrameLayout.LayoutParams )child.getLayoutParams(); int width = child.getMeasuredWidth(); int height = child.getMeasuredHeight(); int childLeft; int childTop; int gravity = lp.gravity; if (gravity == -1) { gravity = DEFAULT_CHILD_GRAVITY; } int layoutDirection = getResolvedLayoutDirection(); int absoluteGravity = android.view.Gravity.getAbsoluteGravity(gravity, layoutDirection ); int verticalGravity = gravity & android.view.Gravity.VERTICAL_GRAVITY_MASK; switch (absoluteGravity & android.view.Gravity.HORIZONTAL_GRAVITY_MASK) { case android.view.Gravity.LEFT: { childLeft = parentLeft + lp.leftMargin; break; } case android.view.Gravity.CENTER_HORIZONTAL: { childLeft = parentLeft + (parentRight - parentLeft - width) / 2 + lp.leftMargin - lp.rightMargin; break; } case android.view.Gravity.RIGHT: { childLeft = parentRight - width - lp.rightMargin; break; } default: { childLeft = parentLeft + lp.leftMargin; break; } } switch (verticalGravity) { case android.view.Gravity.TOP: { childTop = parentTop + lp.topMargin; break; } case android.view.Gravity.CENTER_VERTICAL: { childTop = parentTop + (parentBottom - parentTop - height) / 2 + lp.topMargin - lp .bottomMargin; break; } case android.view.Gravity.BOTTOM: { childTop = parentBottom - height - lp.bottomMargin; break; } default: { childTop = parentTop + lp.topMargin; break; } } child.layout(childLeft, childTop, childLeft + width, childTop + height); } } } }
protected internal override void onMeasure(int widthMeasureSpec, int heightMeasureSpec ) { int count = getChildCount(); bool measureMatchParentChildren = android.view.View.MeasureSpec.getMode(widthMeasureSpec ) != android.view.View.MeasureSpec.EXACTLY || android.view.View.MeasureSpec.getMode (heightMeasureSpec) != android.view.View.MeasureSpec.EXACTLY; mMatchParentChildren.clear(); int maxHeight = 0; int maxWidth = 0; int childState = 0; { for (int i = 0; i < count; i++) { android.view.View child = getChildAt(i); if (mMeasureAllChildren || child.getVisibility() != GONE) { measureChildWithMargins(child, widthMeasureSpec, 0, heightMeasureSpec, 0); android.widget.FrameLayout.LayoutParams lp = (android.widget.FrameLayout.LayoutParams )child.getLayoutParams(); maxWidth = System.Math.Max(maxWidth, child.getMeasuredWidth() + lp.leftMargin + lp .rightMargin); maxHeight = System.Math.Max(maxHeight, child.getMeasuredHeight() + lp.topMargin + lp.bottomMargin); childState = combineMeasuredStates(childState, child.getMeasuredState()); if (measureMatchParentChildren) { if (lp.width == android.view.ViewGroup.LayoutParams.MATCH_PARENT || lp.height == android.view.ViewGroup.LayoutParams.MATCH_PARENT) { mMatchParentChildren.add(child); } } } } } // Account for padding too maxWidth += getPaddingLeftWithForeground() + getPaddingRightWithForeground(); maxHeight += getPaddingTopWithForeground() + getPaddingBottomWithForeground(); // Check against our minimum height and width maxHeight = System.Math.Max(maxHeight, getSuggestedMinimumHeight()); maxWidth = System.Math.Max(maxWidth, getSuggestedMinimumWidth()); // Check against our foreground's minimum height and width android.graphics.drawable.Drawable drawable = getForeground(); if (drawable != null) { maxHeight = System.Math.Max(maxHeight, drawable.getMinimumHeight()); maxWidth = System.Math.Max(maxWidth, drawable.getMinimumWidth()); } setMeasuredDimension(resolveSizeAndState(maxWidth, widthMeasureSpec, childState), resolveSizeAndState(maxHeight, heightMeasureSpec, childState << MEASURED_HEIGHT_STATE_SHIFT )); count = mMatchParentChildren.size(); if (count > 1) { { for (int i_1 = 0; i_1 < count; i_1++) { android.view.View child = mMatchParentChildren.get(i_1); android.view.ViewGroup.MarginLayoutParams lp = (android.view.ViewGroup.MarginLayoutParams )child.getLayoutParams(); int childWidthMeasureSpec; int childHeightMeasureSpec; if (lp.width == android.view.ViewGroup.LayoutParams.MATCH_PARENT) { childWidthMeasureSpec = android.view.View.MeasureSpec.makeMeasureSpec(getMeasuredWidth () - getPaddingLeftWithForeground() - getPaddingRightWithForeground() - lp.leftMargin - lp.rightMargin, android.view.View.MeasureSpec.EXACTLY); } else { childWidthMeasureSpec = getChildMeasureSpec(widthMeasureSpec, getPaddingLeftWithForeground () + getPaddingRightWithForeground() + lp.leftMargin + lp.rightMargin, lp.width); } if (lp.height == android.view.ViewGroup.LayoutParams.MATCH_PARENT) { childHeightMeasureSpec = android.view.View.MeasureSpec.makeMeasureSpec(getMeasuredHeight () - getPaddingTopWithForeground() - getPaddingBottomWithForeground() - lp.topMargin - lp.bottomMargin, android.view.View.MeasureSpec.EXACTLY); } else { childHeightMeasureSpec = getChildMeasureSpec(heightMeasureSpec, getPaddingTopWithForeground () + getPaddingBottomWithForeground() + lp.topMargin + lp.bottomMargin, lp.height ); } child.measure(childWidthMeasureSpec, childHeightMeasureSpec); } } } }
internal override void measureHorizontal(int widthMeasureSpec, int heightMeasureSpec ) { if (android.view.View.MeasureSpec.getMode(widthMeasureSpec) == android.view.View. MeasureSpec.UNSPECIFIED) { base.measureHorizontal(widthMeasureSpec, heightMeasureSpec); return; } // First, measure with no constraint int unspecifiedWidth = android.view.View.MeasureSpec.makeMeasureSpec(0, android.view.View .MeasureSpec.UNSPECIFIED); mImposedTabsHeight = -1; base.measureHorizontal(unspecifiedWidth, heightMeasureSpec); int extraWidth = getMeasuredWidth() - android.view.View.MeasureSpec.getSize(widthMeasureSpec ); if (extraWidth > 0) { int count = getChildCount(); int childCount = 0; { for (int i = 0; i < count; i++) { android.view.View child = getChildAt(i); if (child.getVisibility() == GONE) { continue; } childCount++; } } if (childCount > 0) { if (mImposedTabWidths == null || mImposedTabWidths.Length != count) { mImposedTabWidths = new int[count]; } { for (int i_1 = 0; i_1 < count; i_1++) { android.view.View child = getChildAt(i_1); if (child.getVisibility() == GONE) { continue; } int childWidth = child.getMeasuredWidth(); int delta = extraWidth / childCount; int newWidth = System.Math.Max(0, childWidth - delta); mImposedTabWidths[i_1] = newWidth; // Make sure the extra width is evenly distributed, no int division remainder extraWidth -= childWidth - newWidth; // delta may have been clamped childCount--; mImposedTabsHeight = System.Math.Max(mImposedTabsHeight, child.getMeasuredHeight( )); } } } } // Measure again, this time with imposed tab widths and respecting initial spec request base.measureHorizontal(widthMeasureSpec, heightMeasureSpec); }