diff options
author | Fabrice Di Meglio <fdimeglio@google.com> | 2011-06-30 13:11:50 -0700 |
---|---|---|
committer | Android (Google) Code Review <android-gerrit@google.com> | 2011-06-30 13:11:50 -0700 |
commit | fe8b06cfb4cb4991bcfd09ffa60dcbb7b48ee45f (patch) | |
tree | d65de3cfe4a66df8d0fc8d4892b9d0d5effac3cd /core | |
parent | 169abcf63fd5976330d21f5a0e4f808d63aba038 (diff) | |
parent | c0ccf0c47c00942a9d0f2670600a8f2d4d7adb5b (diff) | |
download | frameworks_base-fe8b06cfb4cb4991bcfd09ffa60dcbb7b48ee45f.zip frameworks_base-fe8b06cfb4cb4991bcfd09ffa60dcbb7b48ee45f.tar.gz frameworks_base-fe8b06cfb4cb4991bcfd09ffa60dcbb7b48ee45f.tar.bz2 |
Merge "Support control of text alignment."
Diffstat (limited to 'core')
-rw-r--r-- | core/java/android/text/Layout.java | 44 | ||||
-rw-r--r-- | core/java/android/view/View.java | 10 | ||||
-rw-r--r-- | core/java/android/view/ViewGroup.java | 6 | ||||
-rw-r--r-- | core/java/android/widget/TextView.java | 97 |
4 files changed, 121 insertions, 36 deletions
diff --git a/core/java/android/text/Layout.java b/core/java/android/text/Layout.java index 4107c5a..aae9ccf 100644 --- a/core/java/android/text/Layout.java +++ b/core/java/android/text/Layout.java @@ -1,4 +1,4 @@ -/* + /* * Copyright (C) 2006 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -266,7 +266,7 @@ public abstract class Layout { } } - Alignment align = mAlignment; + Alignment paraAlign = mAlignment; TabStops tabStops = null; boolean tabStopsIsInitialized = false; @@ -310,10 +310,10 @@ public abstract class Layout { ParagraphStyle.class); spans = getParagraphSpans(sp, start, spanEnd, ParagraphStyle.class); - align = mAlignment; + paraAlign = mAlignment; for (int n = spans.length-1; n >= 0; n--) { if (spans[n] instanceof AlignmentSpan) { - align = ((AlignmentSpan) spans[n]).getAlignment(); + paraAlign = ((AlignmentSpan) spans[n]).getAlignment(); break; } } @@ -360,6 +360,16 @@ public abstract class Layout { tabStopsIsInitialized = true; } + // Determine whether the line aligns to normal, opposite, or center. + Alignment align = paraAlign; + if (align == Alignment.ALIGN_LEFT) { + align = (dir == DIR_LEFT_TO_RIGHT) ? + Alignment.ALIGN_NORMAL : Alignment.ALIGN_OPPOSITE; + } else if (align == Alignment.ALIGN_RIGHT) { + align = (dir == DIR_LEFT_TO_RIGHT) ? + Alignment.ALIGN_OPPOSITE : Alignment.ALIGN_NORMAL; + } + int x; if (align == Alignment.ALIGN_NORMAL) { if (dir == DIR_LEFT_TO_RIGHT) { @@ -411,7 +421,9 @@ public abstract class Layout { int dir = getParagraphDirection(line); int x; - if (align == Alignment.ALIGN_NORMAL) { + if (align == Alignment.ALIGN_LEFT) { + x = left; + } else if (align == Alignment.ALIGN_NORMAL) { if (dir == DIR_LEFT_TO_RIGHT) { x = left; } else { @@ -430,7 +442,9 @@ public abstract class Layout { } } int max = (int)getLineExtent(line, tabStops, false); - if (align == Alignment.ALIGN_OPPOSITE) { + if (align == Alignment.ALIGN_RIGHT) { + x = right - max; + } else if (align == Alignment.ALIGN_OPPOSITE) { if (dir == DIR_LEFT_TO_RIGHT) { x = right - max; } else { @@ -738,11 +752,15 @@ public abstract class Layout { int dir = getParagraphDirection(line); Alignment align = getParagraphAlignment(line); - if (align == Alignment.ALIGN_NORMAL) { + if (align == Alignment.ALIGN_LEFT) { + return 0; + } else if (align == Alignment.ALIGN_NORMAL) { if (dir == DIR_RIGHT_TO_LEFT) return getParagraphRight(line) - getLineMax(line); else return 0; + } else if (align == Alignment.ALIGN_RIGHT) { + return mWidth - getLineMax(line); } else if (align == Alignment.ALIGN_OPPOSITE) { if (dir == DIR_RIGHT_TO_LEFT) return 0; @@ -765,11 +783,15 @@ public abstract class Layout { int dir = getParagraphDirection(line); Alignment align = getParagraphAlignment(line); - if (align == Alignment.ALIGN_NORMAL) { + if (align == Alignment.ALIGN_LEFT) { + return getParagraphLeft(line) + getLineMax(line); + } else if (align == Alignment.ALIGN_NORMAL) { if (dir == DIR_RIGHT_TO_LEFT) return mWidth; else return getParagraphLeft(line) + getLineMax(line); + } else if (align == Alignment.ALIGN_RIGHT) { + return mWidth; } else if (align == Alignment.ALIGN_OPPOSITE) { if (dir == DIR_RIGHT_TO_LEFT) return getLineMax(line); @@ -1765,8 +1787,10 @@ public abstract class Layout { ALIGN_NORMAL, ALIGN_OPPOSITE, ALIGN_CENTER, - // XXX ALIGN_LEFT, - // XXX ALIGN_RIGHT, + /** @hide */ + ALIGN_LEFT, + /** @hide */ + ALIGN_RIGHT, } private static final int TAB_INCREMENT = 20; diff --git a/core/java/android/view/View.java b/core/java/android/view/View.java index bf7f359..bb5c954 100644 --- a/core/java/android/view/View.java +++ b/core/java/android/view/View.java @@ -9121,9 +9121,15 @@ public class View implements Drawable.Callback2, KeyEvent.Callback, Accessibilit } /** - * Reset the resolved layout direction by clearing the corresponding flag + * Reset the resolved layout direction. + * + * Subclasses need to override this method to clear cached information that depends on the + * resolved layout direction, or to inform child views that inherit their layout direction. + * Overrides must also call the superclass implementation at the start of their implementation. + * + * @hide */ - void resetLayoutDirectionResolution() { + protected void resetLayoutDirectionResolution() { // Reset the current View resolution mPrivateFlags2 &= ~LAYOUT_DIRECTION_RESOLVED; } diff --git a/core/java/android/view/ViewGroup.java b/core/java/android/view/ViewGroup.java index 4beabfd..da88fbb 100644 --- a/core/java/android/view/ViewGroup.java +++ b/core/java/android/view/ViewGroup.java @@ -4999,12 +4999,8 @@ public abstract class ViewGroup extends View implements ViewParent, ViewManager viewAncestor.requestTransitionStart(transition); } - /** - * This method will be called when we need to reset the layout direction resolution flag - * - */ @Override - void resetLayoutDirectionResolution() { + protected void resetLayoutDirectionResolution() { super.resetLayoutDirectionResolution(); // Take care of resetting the children resolution too diff --git a/core/java/android/widget/TextView.java b/core/java/android/widget/TextView.java index 2a70ac8..939779f 100644 --- a/core/java/android/widget/TextView.java +++ b/core/java/android/widget/TextView.java @@ -340,6 +340,16 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener private WordIterator mWordIterator; + // The alignment to pass to Layout, or null if not resolved. + private Layout.Alignment mLayoutAlignment; + + // The default value for mTextAlign. + private TextAlign mTextAlign = TextAlign.INHERIT; + + private static enum TextAlign { + INHERIT, GRAVITY, TEXT_START, TEXT_END, CENTER, VIEW_START, VIEW_END; + } + /* * Kick-start the font cache for the zygote process (to pay the cost of * initializing freetype for our default font only once). @@ -5532,6 +5542,73 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener physicalWidth, false); } + @Override + protected void resetLayoutDirectionResolution() { + super.resetLayoutDirectionResolution(); + + if (mLayoutAlignment != null && + (mTextAlign == TextAlign.VIEW_START || + mTextAlign == TextAlign.VIEW_END)) { + mLayoutAlignment = null; + } + } + + private Layout.Alignment getLayoutAlignment() { + if (mLayoutAlignment == null) { + Layout.Alignment alignment; + TextAlign textAlign = mTextAlign; + switch (textAlign) { + case INHERIT: + // fall through to gravity temporarily + // intention is to inherit value through view hierarchy. + case GRAVITY: + switch (mGravity & Gravity.RELATIVE_HORIZONTAL_GRAVITY_MASK) { + case Gravity.START: + alignment = Layout.Alignment.ALIGN_NORMAL; + break; + case Gravity.END: + alignment = Layout.Alignment.ALIGN_OPPOSITE; + break; + case Gravity.LEFT: + alignment = Layout.Alignment.ALIGN_LEFT; + break; + case Gravity.RIGHT: + alignment = Layout.Alignment.ALIGN_RIGHT; + break; + case Gravity.CENTER_HORIZONTAL: + alignment = Layout.Alignment.ALIGN_CENTER; + break; + default: + alignment = Layout.Alignment.ALIGN_NORMAL; + break; + } + break; + case TEXT_START: + alignment = Layout.Alignment.ALIGN_NORMAL; + break; + case TEXT_END: + alignment = Layout.Alignment.ALIGN_OPPOSITE; + break; + case CENTER: + alignment = Layout.Alignment.ALIGN_CENTER; + break; + case VIEW_START: + alignment = (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) ? + Layout.Alignment.ALIGN_RIGHT : Layout.Alignment.ALIGN_LEFT; + break; + case VIEW_END: + alignment = (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) ? + Layout.Alignment.ALIGN_LEFT : Layout.Alignment.ALIGN_RIGHT; + break; + default: + alignment = Layout.Alignment.ALIGN_NORMAL; + break; + } + mLayoutAlignment = alignment; + } + return mLayoutAlignment; + } + /** * The width passed in is now the desired layout width, * not the full view width with padding. @@ -5552,25 +5629,7 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener hintWidth = 0; } - final int layoutDirection = getResolvedLayoutDirection(); - final int absoluteGravity = Gravity.getAbsoluteGravity(mGravity, layoutDirection); - - Layout.Alignment alignment; - switch (absoluteGravity & Gravity.HORIZONTAL_GRAVITY_MASK) { - case Gravity.CENTER_HORIZONTAL: - alignment = Layout.Alignment.ALIGN_CENTER; - break; - - case Gravity.RIGHT: - // Note, Layout resolves ALIGN_OPPOSITE to left or - // right based on the paragraph direction. - alignment = Layout.Alignment.ALIGN_OPPOSITE; - break; - - default: - alignment = Layout.Alignment.ALIGN_NORMAL; - } - + Layout.Alignment alignment = getLayoutAlignment(); boolean shouldEllipsize = mEllipsize != null && mInput == null; if (mText instanceof Spannable) { |