下面列出了android.text.Layout#Alignment ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* @return True, if adjustments were made that require the view to be invalidated.
*/
private boolean adjustBottomLines() {
// Bail out if we have a zero width; lines will be adjusted during next layout.
if (getWidth() == 0) {
return false;
}
int destBottomLines;
textPaint.setTextSize(bottomTextSize);
if (tempErrorText != null || helperText != null) {
Layout.Alignment alignment = (getGravity() & Gravity.RIGHT) == Gravity.RIGHT || isRTL() ?
Layout.Alignment.ALIGN_OPPOSITE : (getGravity() & Gravity.LEFT) == Gravity.LEFT ?
Layout.Alignment.ALIGN_NORMAL : Layout.Alignment.ALIGN_CENTER;
textLayout = new StaticLayout(tempErrorText != null ? tempErrorText : helperText, textPaint, getWidth() - getBottomTextLeftOffset() - getBottomTextRightOffset() - getPaddingLeft() - getPaddingRight(), alignment, 1.0f, 0.0f, true);
destBottomLines = Math.max(textLayout.getLineCount(), minBottomTextLines);
} else {
destBottomLines = minBottomLines;
}
if (bottomLines != destBottomLines) {
getBottomLinesAnimator(destBottomLines).start();
}
bottomLines = destBottomLines;
return true;
}
/**
* @return True, if adjustments were made that require the view to be invalidated.
*/
private boolean adjustBottomLines() {
// Bail out if we have a zero width; lines will be adjusted during next layout.
if (getWidth() == 0) {
return false;
}
int destBottomLines;
textPaint.setTextSize(bottomTextSize);
if (tempErrorText != null || helperText != null) {
Layout.Alignment alignment = (getGravity() & Gravity.RIGHT) == Gravity.RIGHT || isRTL() ?
Layout.Alignment.ALIGN_OPPOSITE : (getGravity() & Gravity.LEFT) == Gravity.LEFT ?
Layout.Alignment.ALIGN_NORMAL : Layout.Alignment.ALIGN_CENTER;
textLayout = new StaticLayout(tempErrorText != null ? tempErrorText : helperText, textPaint, getWidth() - getBottomTextLeftOffset() - getBottomTextRightOffset() - getPaddingLeft() - getPaddingRight(), alignment, 1.0f, 0.0f, true);
destBottomLines = Math.max(textLayout.getLineCount(), minBottomTextLines);
} else {
destBottomLines = minBottomLines;
}
if (bottomLines != destBottomLines) {
getBottomLinesAnimator(destBottomLines).start();
}
bottomLines = destBottomLines;
return true;
}
/**
* @return True, if adjustments were made that require the view to be invalidated.
*/
private boolean adjustBottomLines() {
// Bail out if we have a zero width; lines will be adjusted during next layout.
if (getWidth() == 0) {
return false;
}
int destBottomLines;
textPaint.setTextSize(bottomTextSize);
if (tempErrorText != null || helperText != null) {
Layout.Alignment alignment = (getGravity() & Gravity.RIGHT) == Gravity.RIGHT || isRTL() ?
Layout.Alignment.ALIGN_OPPOSITE : (getGravity() & Gravity.LEFT) == Gravity.LEFT ?
Layout.Alignment.ALIGN_NORMAL : Layout.Alignment.ALIGN_CENTER;
textLayout = new StaticLayout(tempErrorText != null ? tempErrorText : helperText, textPaint, getWidth() - getBottomTextLeftOffset() - getBottomTextRightOffset() - getPaddingLeft() - getPaddingRight(), alignment, 1.0f, 0.0f, true);
destBottomLines = Math.max(textLayout.getLineCount(), minBottomTextLines);
} else {
destBottomLines = minBottomLines;
}
if (bottomLines != destBottomLines) {
getBottomLinesAnimator(destBottomLines).start();
}
bottomLines = destBottomLines;
return true;
}
/**
* @return True, if adjustments were made that require the view to be invalidated.
*/
private boolean adjustBottomLines() {
// Bail out if we have a zero width; lines will be adjusted during next layout.
if (getWidth() == 0) {
return false;
}
int destBottomLines;
textPaint.setTextSize(bottomTextSize);
if (tempErrorText != null || helperText != null) {
Layout.Alignment alignment = (getGravity() & Gravity.RIGHT) == Gravity.RIGHT || isRTL() ?
Layout.Alignment.ALIGN_OPPOSITE : (getGravity() & Gravity.LEFT) == Gravity.LEFT ?
Layout.Alignment.ALIGN_NORMAL : Layout.Alignment.ALIGN_CENTER;
textLayout = new StaticLayout(tempErrorText != null ? tempErrorText : helperText, textPaint, getWidth() - getBottomTextLeftOffset() - getBottomTextRightOffset() - getPaddingLeft() - getPaddingRight(), alignment, 1.0f, 0.0f, true);
destBottomLines = Math.max(textLayout.getLineCount(), minBottomTextLines);
} else {
destBottomLines = minBottomLines;
}
if (bottomLines != destBottomLines) {
getBottomLinesAnimator(destBottomLines).start();
}
bottomLines = destBottomLines;
return true;
}
/**
* @return True, if adjustments were made that require the view to be invalidated.
*/
private boolean adjustBottomLines() {
// Bail out if we have a zero width; lines will be adjusted during next layout.
if (getWidth() == 0) {
return false;
}
int destBottomLines;
textPaint.setTextSize(bottomTextSize);
if (tempErrorText != null || helperText != null) {
Layout.Alignment alignment = (getGravity() & Gravity.RIGHT) == Gravity.RIGHT || isRTL() ?
Layout.Alignment.ALIGN_OPPOSITE : (getGravity() & Gravity.LEFT) == Gravity.LEFT ?
Layout.Alignment.ALIGN_NORMAL : Layout.Alignment.ALIGN_CENTER;
textLayout = new StaticLayout(tempErrorText != null ? tempErrorText : helperText, textPaint, getWidth() - getBottomTextLeftOffset() - getBottomTextRightOffset() - getPaddingLeft() - getPaddingRight(), alignment, 1.0f, 0.0f, true);
destBottomLines = Math.max(textLayout.getLineCount(), minBottomTextLines);
} else {
destBottomLines = minBottomLines;
}
if (bottomLines != destBottomLines) {
getBottomLinesAnimator(destBottomLines).start();
}
bottomLines = destBottomLines;
return true;
}
private static Layout.Alignment getAlignmentByGravity(int gravity) {
switch (gravity & Gravity.RELATIVE_HORIZONTAL_GRAVITY_MASK) {
case Gravity.START:
return Layout.Alignment.ALIGN_NORMAL;
case Gravity.END:
return Layout.Alignment.ALIGN_OPPOSITE;
case Gravity.LEFT:
return Layout.Alignment.ALIGN_LEFT;
case Gravity.RIGHT:
return Layout.Alignment.ALIGN_RIGHT;
case Gravity.CENTER_HORIZONTAL:
return Layout.Alignment.ALIGN_CENTER;
default:
return Layout.Alignment.ALIGN_NORMAL;
}
}
private void endDiv() {
int end = mResult.length();
Object obj = getLast(mResult, Div.class);
int start = mResult.getSpanStart(obj);
mResult.removeSpan(obj);
if (start != end) {
if (!checkDuplicateSpan(mResult, start, AlignmentSpan.class)) {
Div divObj = (Div) obj;
Layout.Alignment align = divObj.mAlign.equalsIgnoreCase("center") ? Layout.Alignment.ALIGN_CENTER :
divObj.mAlign.equalsIgnoreCase("right") ? Layout.Alignment.ALIGN_OPPOSITE : Layout.Alignment.ALIGN_NORMAL;
if (align != null) {
if (mResult.charAt(end - 1) != '\n') {
// yes we need that linefeed, or we will get crashes
mResult.append('\n');
}
// use SPAN_EXCLUSIVE_EXCLUSIVE here, will be replaced later anyway when the cleanup function is called
boolean isRTL = Helper.isRTL(mResult, start, end);
mResult.setSpan(new AlignmentSpan(align, isRTL), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
}
}
}
private void drawIndicatorsTextAbove(Canvas canvas, String text, TextPaint paintText, float x, float y, Layout.Alignment alignment) {
final float textHeight = calculateTextMultilineHeight(text, paintText);
y -= textHeight;
final int width = (int) paintText.measureText(text);
if (x >= getWidth() - settings.paddingCorners) {
x = (getWidth() - width - settings.paddingCorners / 2f);
} else if (x <= 0) {
x = width / 2f;
} else {
x = (x - width / 2f);
}
if (x < 0) {
x = 0;
}
if (x + width > getWidth()) {
x = getWidth() - width;
}
drawText(canvas, text, x, y, paintText, alignment);
}
private void drawText(Canvas canvas, String text, float x, float y, TextPaint paint, Layout.Alignment aligment) {
canvas.save();
{
canvas.translate(x, y);
final StaticLayout staticLayout = new StaticLayout(text, paint, (int) paint.measureText(text), aligment, 1.0f, 0, false);
staticLayout.draw(canvas);
}
canvas.restore();
}
private static void encodeTextAlignmentByDiv(StringBuilder out, Spanned text, int option) {
int len = text.length();
int next;
for (int i = 0; i < len; i = next) {
next = text.nextSpanTransition(i, len, ParagraphStyle.class);
ParagraphStyle[] style = text.getSpans(i, next, ParagraphStyle.class);
String elements = " ";
boolean needDiv = false;
for (int j = 0; j < style.length; j++) {
if (style[j] instanceof AlignmentSpan) {
Layout.Alignment align =
((AlignmentSpan) style[j]).getAlignment();
needDiv = true;
if (align == Layout.Alignment.ALIGN_CENTER) {
elements = "align=\"center\" " + elements;
} else if (align == Layout.Alignment.ALIGN_OPPOSITE) {
elements = "align=\"right\" " + elements;
} else {
elements = "align=\"left\" " + elements;
}
}
}
if (needDiv) {
out.append("<div ").append(elements).append(">");
}
withinDiv(out, text, i, next, option);
if (needDiv) {
out.append("</div>");
}
}
}
@Override
public void setAlignment(Layout.Alignment alignment) {
if (mAlignLeft != null) mAlignLeft.setChecked(alignment == Layout.Alignment.ALIGN_NORMAL);
if (mAlignCenter != null)
mAlignCenter.setChecked(alignment == Layout.Alignment.ALIGN_CENTER);
if (mAlignRight != null)
mAlignRight.setChecked(alignment == Layout.Alignment.ALIGN_OPPOSITE);
}
private void drawText(Canvas canvas, String text, float x, float y, TextPaint paint, Layout.Alignment aligment) {
canvas.save();
{
canvas.translate(x, y);
final StaticLayout staticLayout = new StaticLayout(text, paint, (int) paint.measureText(text), aligment, 1.0f, 0, false);
staticLayout.draw(canvas);
}
canvas.restore();
}
private static void withinHtml(StringBuilder out, Spanned text) {
int len = text.length();
int next;
for (int i = 0; i < text.length(); i = next) {
next = text.nextSpanTransition(i, len, ParagraphStyle.class);
ParagraphStyle[] style = text.getSpans(i, next, ParagraphStyle.class);
String elements = " ";
boolean needDiv = false;
for(int j = 0; j < style.length; j++) {
if (style[j] instanceof AlignmentSpan) {
Layout.Alignment align =
((AlignmentSpan) style[j]).getAlignment();
needDiv = true;
if (align == Layout.Alignment.ALIGN_CENTER) {
elements = "align=\"center\" " + elements;
} else if (align == Layout.Alignment.ALIGN_OPPOSITE) {
elements = "align=\"right\" " + elements;
} else {
elements = "align=\"left\" " + elements;
}
}
}
if (needDiv) {
out.append("<div ").append(elements).append(">");
}
withinDiv(out, text, i, next);
if (needDiv) {
out.append("</div>");
}
}
}
public Layout.Alignment getTextAlign() {
return textAlign;
}
public WebvttCssStyle setTextAlign(Layout.Alignment textAlign) {
this.textAlign = textAlign;
return this;
}
public static Layout.Alignment ALIGN_LEFT() {
return alignments.length >= 5 ? alignments[3] : Layout.Alignment.ALIGN_NORMAL;
}
@OnMount
static void onMount(
final ComponentContext c,
EditTextWithEventHandlers editText,
@Prop(optional = true, resType = ResType.STRING) CharSequence text,
@Prop(optional = true, resType = ResType.STRING) CharSequence initialText,
@Prop(optional = true, resType = ResType.STRING) CharSequence hint,
@Prop(optional = true) TextUtils.TruncateAt ellipsize,
@Prop(optional = true, resType = ResType.INT) int minLines,
@Prop(optional = true, resType = ResType.INT) int maxLines,
@Prop(optional = true, resType = ResType.INT) int maxLength,
@Prop(optional = true, resType = ResType.DIMEN_OFFSET) float shadowRadius,
@Prop(optional = true, resType = ResType.DIMEN_OFFSET) float shadowDx,
@Prop(optional = true, resType = ResType.DIMEN_OFFSET) float shadowDy,
@Prop(optional = true, resType = ResType.COLOR) int shadowColor,
@Prop(optional = true, resType = ResType.BOOL) boolean isSingleLine,
@Prop(optional = true, resType = ResType.COLOR) int textColor,
@Prop(optional = true) ColorStateList textColorStateList,
@Prop(optional = true, resType = ResType.COLOR) int hintColor,
@Prop(optional = true) ColorStateList hintColorStateList,
@Prop(optional = true, resType = ResType.COLOR) int linkColor,
@Prop(optional = true, resType = ResType.COLOR) int highlightColor,
@Prop(optional = true) ColorStateList tintColorStateList,
@Prop(optional = true, resType = ResType.DIMEN_TEXT) int textSize,
@Prop(optional = true, resType = ResType.DIMEN_OFFSET) float extraSpacing,
@Prop(optional = true, resType = ResType.FLOAT) float spacingMultiplier,
@Prop(optional = true) int textStyle,
@Prop(optional = true) Typeface typeface,
@Prop(optional = true) Layout.Alignment textAlignment,
@Prop(optional = true) int gravity,
@Prop(optional = true) boolean editable,
@Prop(optional = true) int selection,
@Prop(optional = true) int inputType,
@Prop(optional = true) int rawInputType,
@Prop(optional = true) int imeOptions,
@Prop(optional = true) TextView.OnEditorActionListener editorActionListener,
@Prop(optional = true) boolean isSingleLineWrap,
@Prop(optional = true) boolean requestFocus,
@Prop(optional = true) int cursorDrawableRes,
@Prop(optional = true, varArg = "inputFilter") List<InputFilter> inputFilters,
@State AtomicReference<EditTextWithEventHandlers> mountedView,
@State AtomicBoolean configuredInitialText,
@State(canUpdateLazily = true) CharSequence input) {
mountedView.set(editText);
initEditText(
editText,
input == null ? text : input,
// Only set initialText on the EditText during the very first mount.
configuredInitialText.getAndSet(true) ? null : initialText,
hint,
ellipsize,
inputFilters,
minLines,
maxLines,
maxLength,
shadowRadius,
shadowDx,
shadowDy,
shadowColor,
isSingleLine,
textColor,
textColorStateList,
hintColor,
hintColorStateList,
linkColor,
highlightColor,
tintColorStateList,
textSize,
extraSpacing,
spacingMultiplier,
textStyle,
typeface,
textAlignment,
gravity,
editable,
selection,
inputType,
rawInputType,
imeOptions,
editorActionListener,
isSingleLineWrap,
requestFocus,
cursorDrawableRes);
}
public static StaticLayout createStaticLayout(CharSequence source, TextPaint paint, int width, Layout.Alignment align, float spacingmult, float spacingadd, boolean includepad, TextUtils.TruncateAt ellipsize, int ellipsisWidth, int maxLines) {
return createStaticLayout(source, 0, source.length(), paint, width, align, spacingmult, spacingadd, includepad, ellipsize, ellipsisWidth, maxLines);
}
public static StaticLayout createStaticLayout(CharSequence source, TextPaint paint, int width, Layout.Alignment align, float spacingmult, float spacingadd, boolean includepad, TextUtils.TruncateAt ellipsize, int ellipsisWidth, int maxLines) {
return createStaticLayout(source, 0, source.length(), paint, width, align, spacingmult, spacingadd, includepad, ellipsize, ellipsisWidth, maxLines);
}
public static StaticLayout createStaticLayout(CharSequence source, TextPaint paint, int width, Layout.Alignment align, float spacingmult, float spacingadd, boolean includepad, TextUtils.TruncateAt ellipsize, int ellipsisWidth, int maxLines, boolean canContainUrl) {
return createStaticLayout(source, 0, source.length(), paint, width, align, spacingmult, spacingadd, includepad, ellipsize, ellipsisWidth, maxLines, canContainUrl);
}