From 6741c941a76d17c257c396d86c7561aa43005b1a Mon Sep 17 00:00:00 2001 From: Gilles Debunne Date: Tue, 25 Jan 2011 20:11:45 -0800 Subject: Tabs are resized to make sure they fit in their parent. Bug 3365301 This is a first implementation. The text inside the tabs is clipped and can be hardly readable. A nicer complimentary solution is to reduce the 40dip padding in portrait mode. Change-Id: I19439ee6a63c3f9dcdcc944ddbbc18a81949db79 --- core/java/android/widget/TabWidget.java | 50 +++++++++++++++++++++++++++++++ core/java/android/widget/TableLayout.java | 2 +- core/res/res/layout/tab_content.xml | 34 ++++++++++++++------- 3 files changed, 75 insertions(+), 11 deletions(-) (limited to 'core') diff --git a/core/java/android/widget/TabWidget.java b/core/java/android/widget/TabWidget.java index ce4e8e5..51ece33 100644 --- a/core/java/android/widget/TabWidget.java +++ b/core/java/android/widget/TabWidget.java @@ -141,6 +141,55 @@ public class TabWidget extends LinearLayout implements OnFocusChangeListener { } /** + * {@inheritDoc} + */ + @Override + void measureHorizontal(int widthMeasureSpec, int heightMeasureSpec) { + // First measure with no constraint + final int unspecifiedWidth = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED); + super.measureHorizontal(unspecifiedWidth, heightMeasureSpec); + + final int count = getChildCount(); + int totalWidth = 0; + int totalCount = 0; + for (int i = 0; i < count; i++) { + final View child = getChildAt(i); + if (child.getVisibility() == GONE) { + continue; + } + final int childWidth = child.getMeasuredWidth(); + totalWidth += childWidth; + totalCount++; + } + + final int width = MeasureSpec.getSize(widthMeasureSpec); + if (totalWidth > width && totalCount > 0) { + int extraWidth = totalWidth - width; + for (int i = 0; i < count; i++) { + final View child = getChildAt(i); + if (child.getVisibility() == GONE) { + continue; + } + final int childWidth = child.getMeasuredWidth(); + final int delta = extraWidth / totalCount; + final int tabWidth = Math.max(0, childWidth - delta); + + final int tabWidthMeasureSpec = MeasureSpec.makeMeasureSpec( + tabWidth, MeasureSpec.EXACTLY); + final int tabHeightMeasureSpec = MeasureSpec.makeMeasureSpec( + child.getMeasuredHeight(), MeasureSpec.EXACTLY); + + child.measure(tabWidthMeasureSpec, tabHeightMeasureSpec); + + // Make sure the extra width is evenly distributed, avoiding int division remainder + extraWidth -= delta; + totalCount--; + } + setMeasuredDimension(width, getMeasuredHeight()); + } + } + + /** * Returns the tab indicator view at the given index. * * @param index the zero-based index of the tab indicator view to return @@ -175,6 +224,7 @@ public class TabWidget extends LinearLayout implements OnFocusChangeListener { * Sets the drawable to use as a divider between the tab indicators. * @param drawable the divider drawable */ + @Override public void setDividerDrawable(Drawable drawable) { mDividerDrawable = drawable; requestLayout(); diff --git a/core/java/android/widget/TableLayout.java b/core/java/android/widget/TableLayout.java index 7f26e28..842b087 100644 --- a/core/java/android/widget/TableLayout.java +++ b/core/java/android/widget/TableLayout.java @@ -560,7 +560,7 @@ public class TableLayout extends LinearLayout { if ((totalWidth > size) && (mShrinkAllColumns || mShrinkableColumns.size() > 0)) { // oops, the largest columns are wider than the row itself - // fairly redistribute the row's widh among the columns + // fairly redistribute the row's width among the columns mutateColumnsWidth(mShrinkableColumns, mShrinkAllColumns, size, totalWidth); } else if ((totalWidth < size) && (mStretchAllColumns || mStretchableColumns.size() > 0)) { // if we have some space left, we distribute it among the diff --git a/core/res/res/layout/tab_content.xml b/core/res/res/layout/tab_content.xml index 79147fb..3fa4494 100644 --- a/core/res/res/layout/tab_content.xml +++ b/core/res/res/layout/tab_content.xml @@ -18,16 +18,30 @@ */ --> - - - - + + + + + + - + + -- cgit v1.1