diff options
| author | Adam Powell <adamp@google.com> | 2010-11-30 21:26:29 -0800 |
|---|---|---|
| committer | Adam Powell <adamp@google.com> | 2010-12-01 23:23:21 -0800 |
| commit | fcca00accb923d3cbda4e0d6f5540b10e8279cd2 (patch) | |
| tree | 6e535a6329a7c1f70dcdd4820fe41aed445d1914 /core/java/android/widget/LinearLayout.java | |
| parent | dcafdb2e9babeaa7aafd1cb15be6423a656de996 (diff) | |
Update themes; dialogs, metrics
Add divider attributes to LinearLayout, plus styles for borderless
buttons. Update text field assets.
Change-Id: I673acab1692cc028a0327e8c154069253a4d52e8
Diffstat (limited to 'core/java/android/widget/LinearLayout.java')
| -rw-r--r-- | core/java/android/widget/LinearLayout.java | 240 |
1 files changed, 237 insertions, 3 deletions
diff --git a/core/java/android/widget/LinearLayout.java b/core/java/android/widget/LinearLayout.java index 1e5489a0fd1e..99b181f6c9a5 100644 --- a/core/java/android/widget/LinearLayout.java +++ b/core/java/android/widget/LinearLayout.java @@ -20,6 +20,8 @@ import com.android.internal.R; import android.content.Context; import android.content.res.TypedArray; +import android.graphics.Canvas; +import android.graphics.drawable.Drawable; import android.util.AttributeSet; import android.view.Gravity; import android.view.View; @@ -57,6 +59,23 @@ public class LinearLayout extends ViewGroup { public static final int VERTICAL = 1; /** + * Don't show any dividers. + */ + public static final int SHOW_DIVIDER_NONE = 0; + /** + * Show a divider at the beginning of the group. + */ + public static final int SHOW_DIVIDER_BEGINNING = 1; + /** + * Show dividers between each item in the group. + */ + public static final int SHOW_DIVIDER_MIDDLE = 2; + /** + * Show a divider at the end of the group. + */ + public static final int SHOW_DIVIDER_END = 4; + + /** * Whether the children of this layout are baseline aligned. Only applicable * if {@link #mOrientation} is horizontal. */ @@ -119,6 +138,12 @@ public class LinearLayout extends ViewGroup { private static final int INDEX_BOTTOM = 2; private static final int INDEX_FILL = 3; + private Drawable mDivider; + private int mDividerWidth; + private int mDividerHeight; + private int mShowDividers; + private int mDividerPadding; + public LinearLayout(Context context) { super(context); } @@ -155,10 +180,158 @@ public class LinearLayout extends ViewGroup { mUseLargestChild = a.getBoolean(R.styleable.LinearLayout_measureWithLargestChild, false); + setDividerDrawable(a.getDrawable(R.styleable.LinearLayout_divider)); + mShowDividers = a.getInt(R.styleable.LinearLayout_showDividers, SHOW_DIVIDER_NONE); + mDividerPadding = a.getDimensionPixelSize(R.styleable.LinearLayout_dividerPadding, 0); + a.recycle(); } /** + * Set how dividers should be shown between items in this layout + * + * @param showDividers One or more of {@link #SHOW_DIVIDER_BEGINNING}, + * {@link #SHOW_DIVIDER_MIDDLE}, or {@link #SHOW_DIVIDER_END}, + * or {@link #SHOW_DIVIDER_NONE} to show no dividers. + */ + public void setShowDividers(int showDividers) { + if (showDividers != mShowDividers) { + requestLayout(); + } + mShowDividers = showDividers; + } + + /** + * @return A flag set indicating how dividers should be shown around items. + * @see #setShowDividers(int) + */ + public int getShowDividers() { + return mShowDividers; + } + + /** + * Set a drawable to be used as a divider between items. + * @param divider Drawable that will divide each item. + * @see #setShowDividers(int) + */ + public void setDividerDrawable(Drawable divider) { + if (divider == mDivider) { + return; + } + mDivider = divider; + if (divider != null) { + mDividerWidth = divider.getIntrinsicWidth(); + mDividerHeight = divider.getIntrinsicHeight(); + } else { + mDividerWidth = 0; + mDividerHeight = 0; + } + setWillNotDraw(divider == null); + requestLayout(); + } + + @Override + protected void onDraw(Canvas canvas) { + if (mDivider == null) { + return; + } + + if (mOrientation == VERTICAL) { + drawDividersVertical(canvas); + } else { + drawDividersHorizontal(canvas); + } + } + + void drawDividersVertical(Canvas canvas) { + final boolean showDividerBeginning = + (mShowDividers & SHOW_DIVIDER_BEGINNING) == SHOW_DIVIDER_BEGINNING; + final boolean showDividerMiddle = + (mShowDividers & SHOW_DIVIDER_MIDDLE) == SHOW_DIVIDER_MIDDLE; + final boolean showDividerEnd = + (mShowDividers & SHOW_DIVIDER_END) == SHOW_DIVIDER_END; + + final int count = getVirtualChildCount(); + int top = getPaddingTop(); + boolean firstVisible = true; + for (int i = 0; i < count; i++) { + final View child = getVirtualChildAt(i); + + if (child == null) { + top += measureNullChild(i); + } else if (child.getVisibility() != GONE) { + if (firstVisible) { + firstVisible = false; + if (showDividerBeginning) { + drawHorizontalDivider(canvas, top); + top += mDividerHeight; + } + } else if (showDividerMiddle) { + drawHorizontalDivider(canvas, top); + top += mDividerHeight; + } + + LayoutParams lp = (LayoutParams) child.getLayoutParams(); + top += lp.topMargin + child.getHeight() + lp.bottomMargin; + } + } + + if (showDividerEnd) { + drawHorizontalDivider(canvas, top); + } + } + + void drawDividersHorizontal(Canvas canvas) { + final boolean showDividerBeginning = + (mShowDividers & SHOW_DIVIDER_BEGINNING) == SHOW_DIVIDER_BEGINNING; + final boolean showDividerMiddle = + (mShowDividers & SHOW_DIVIDER_MIDDLE) == SHOW_DIVIDER_MIDDLE; + final boolean showDividerEnd = + (mShowDividers & SHOW_DIVIDER_END) == SHOW_DIVIDER_END; + + final int count = getVirtualChildCount(); + int left = getPaddingLeft(); + boolean firstVisible = true; + for (int i = 0; i < count; i++) { + final View child = getVirtualChildAt(i); + + if (child == null) { + left += measureNullChild(i); + } else if (child.getVisibility() != GONE) { + if (firstVisible) { + firstVisible = false; + if (showDividerBeginning) { + drawVerticalDivider(canvas, left); + left += mDividerWidth; + } + } else if (showDividerMiddle) { + drawVerticalDivider(canvas, left); + left += mDividerWidth; + } + + LayoutParams lp = (LayoutParams) child.getLayoutParams(); + left += lp.leftMargin + child.getWidth() + lp.rightMargin; + } + } + + if (showDividerEnd) { + drawVerticalDivider(canvas, left); + } + } + + void drawHorizontalDivider(Canvas canvas, int top) { + mDivider.setBounds(getPaddingLeft() + mDividerPadding, top, + getWidth() - getPaddingRight() - mDividerPadding, top + mDividerHeight); + mDivider.draw(canvas); + } + + void drawVerticalDivider(Canvas canvas, int left) { + mDivider.setBounds(left, getPaddingTop() + mDividerPadding, + left + mDividerWidth, getHeight() - getPaddingBottom() - mDividerPadding); + mDivider.draw(canvas); + } + + /** * <p>Indicates whether widgets contained within this layout are aligned * on their baseline or not.</p> * @@ -380,7 +553,14 @@ public class LinearLayout extends ViewGroup { int largestChildHeight = Integer.MIN_VALUE; + // A divider at the end will change how much space views can consume. + final boolean showDividerBeginning = + (mShowDividers & SHOW_DIVIDER_BEGINNING) == SHOW_DIVIDER_BEGINNING; + final boolean showDividerMiddle = + (mShowDividers & SHOW_DIVIDER_MIDDLE) == SHOW_DIVIDER_MIDDLE; + // See how tall everyone is. Also remember max width. + boolean firstVisible = true; for (int i = 0; i < count; ++i) { final View child = getVirtualChildAt(i); @@ -394,6 +574,15 @@ public class LinearLayout extends ViewGroup { continue; } + if (firstVisible) { + firstVisible = false; + if (showDividerBeginning) { + mTotalLength += mDividerHeight; + } + } else if (showDividerMiddle) { + mTotalLength += mDividerHeight; + } + LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) child.getLayoutParams(); totalWeight += lp.weight; @@ -486,6 +675,10 @@ public class LinearLayout extends ViewGroup { i += getChildrenSkipCount(child, i); } + if (mTotalLength > 0 && (mShowDividers & SHOW_DIVIDER_END) == SHOW_DIVIDER_END) { + mTotalLength += mDividerHeight; + } + if (useLargestChild && heightMode == MeasureSpec.AT_MOST) { mTotalLength = 0; @@ -679,7 +872,14 @@ public class LinearLayout extends ViewGroup { int largestChildWidth = Integer.MIN_VALUE; + // A divider at the end will change how much space views can consume. + final boolean showDividerBeginning = + (mShowDividers & SHOW_DIVIDER_BEGINNING) == SHOW_DIVIDER_BEGINNING; + final boolean showDividerMiddle = + (mShowDividers & SHOW_DIVIDER_MIDDLE) == SHOW_DIVIDER_MIDDLE; + // See how wide everyone is. Also remember max height. + boolean firstVisible = true; for (int i = 0; i < count; ++i) { final View child = getVirtualChildAt(i); @@ -693,6 +893,15 @@ public class LinearLayout extends ViewGroup { continue; } + if (firstVisible) { + firstVisible = false; + if (showDividerBeginning) { + mTotalLength += mDividerWidth; + } + } else if (showDividerMiddle) { + mTotalLength += mDividerWidth; + } + final LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) child.getLayoutParams(); @@ -803,6 +1012,10 @@ public class LinearLayout extends ViewGroup { i += getChildrenSkipCount(child, i); } + if (mTotalLength > 0 && (mShowDividers & SHOW_DIVIDER_END) == SHOW_DIVIDER_END) { + mTotalLength += mDividerWidth; + } + // Check mMaxAscent[INDEX_TOP] first because it maps to Gravity.TOP, // the most common case if (maxAscent[INDEX_TOP] != -1 || @@ -1127,7 +1340,14 @@ public class LinearLayout extends ViewGroup { } } - + + final boolean showDividerMiddle = + (mShowDividers & SHOW_DIVIDER_MIDDLE) == SHOW_DIVIDER_MIDDLE; + + if ((mShowDividers & SHOW_DIVIDER_BEGINNING) == SHOW_DIVIDER_BEGINNING) { + childTop += mDividerHeight; + } + for (int i = 0; i < count; i++) { final View child = getVirtualChildAt(i); if (child == null) { @@ -1162,12 +1382,15 @@ public class LinearLayout extends ViewGroup { break; } - childTop += lp.topMargin; setChildFrame(child, childLeft, childTop + getLocationOffset(child), childWidth, childHeight); childTop += childHeight + lp.bottomMargin + getNextLocationOffset(child); + if (showDividerMiddle) { + childTop += mDividerHeight; + } + i += getChildrenSkipCount(child, i); } } @@ -1216,7 +1439,14 @@ public class LinearLayout extends ViewGroup { childLeft += ((mRight - mLeft) - mTotalLength) / 2; break; } - } + } + + final boolean showDividerMiddle = + (mShowDividers & SHOW_DIVIDER_MIDDLE) == SHOW_DIVIDER_MIDDLE; + + if ((mShowDividers & SHOW_DIVIDER_BEGINNING) == SHOW_DIVIDER_BEGINNING) { + childLeft += mDividerWidth; + } for (int i = 0; i < count; i++) { final View child = getVirtualChildAt(i); @@ -1282,6 +1512,10 @@ public class LinearLayout extends ViewGroup { childLeft += childWidth + lp.rightMargin + getNextLocationOffset(child); + if (showDividerMiddle) { + childLeft += mDividerWidth; + } + i += getChildrenSkipCount(child, i); } } |
