diff options
| author | Gilles Debunne <debunne@google.com> | 2011-01-25 20:11:45 -0800 |
|---|---|---|
| committer | Gilles Debunne <debunne@google.com> | 2011-01-26 16:01:20 -0800 |
| commit | 6741c941a76d17c257c396d86c7561aa43005b1a (patch) | |
| tree | b09770565acea04341b57bb113666b3bb3a50288 | |
| parent | aaa55d3f45744814907d0809befb49281ced462e (diff) | |
| download | frameworks_base-6741c941a76d17c257c396d86c7561aa43005b1a.zip frameworks_base-6741c941a76d17c257c396d86c7561aa43005b1a.tar.gz frameworks_base-6741c941a76d17c257c396d86c7561aa43005b1a.tar.bz2 | |
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
| -rw-r--r-- | core/java/android/widget/TabWidget.java | 50 | ||||
| -rw-r--r-- | core/java/android/widget/TableLayout.java | 2 | ||||
| -rw-r--r-- | core/res/res/layout/tab_content.xml | 34 |
3 files changed, 75 insertions, 11 deletions
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 @@ */ --> -<TabHost xmlns:android="http://schemas.android.com/apk/res/android" android:id="@android:id/tabhost" - android:layout_width="match_parent" android:layout_height="match_parent"> - <LinearLayout android:orientation="vertical" - android:layout_width="match_parent" android:layout_height="match_parent"> - <TabWidget android:id="@android:id/tabs" - android:orientation="horizontal" android:layout_width="match_parent" - android:layout_height="wrap_content" android:layout_weight="0" /> - <FrameLayout android:id="@android:id/tabcontent" - android:layout_width="match_parent" android:layout_height="0dip" +<TabHost + xmlns:android="http://schemas.android.com/apk/res/android" + android:id="@android:id/tabhost" + android:layout_width="match_parent" + android:layout_height="match_parent"> + + <LinearLayout + android:orientation="vertical" + android:layout_width="match_parent" + android:layout_height="match_parent"> + + <TabWidget + android:id="@android:id/tabs" + android:orientation="horizontal" + android:layout_width="match_parent" + android:layout_height="wrap_content" + android:layout_weight="0"/> + + <FrameLayout + android:id="@android:id/tabcontent" + android:layout_width="match_parent" + android:layout_height="0dip" android:layout_weight="1"/> - </LinearLayout> + + </LinearLayout> </TabHost> |
