请问各位有没有关于TextView更好的排版方法?要左右对齐
首先先上我这里的代码:
在判断标点符号的时候是把全角变为半角,当显示不完全时,会用......省略号代替import java.util.ArrayList;
import java.util.List;import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.text.Layout;
import android.text.Layout.Alignment;
import android.text.StaticLayout;
import android.text.TextUtils.TruncateAt;
import android.util.AttributeSet;
import android.widget.TextView;public class EllipsizingTextView extends TextView {
    private static final String TAG = "EllipsizeText";
private static final boolean enableLog = (false && Const.DEBUG);
private static final String ELLIPSIS = "......"; public interface EllipsizeListener {
void ellipsizeStateChanged(boolean ellipsized);
} private final List<EllipsizeListener> ellipsizeListeners = new ArrayList<EllipsizeListener>();
private boolean isEllipsized;
private boolean isStale;
private boolean programmaticChange;
private String fullText;
private int maxLines;
private float lineSpacingMultiplier = 1.0f;
private float lineAdditionalVerticalPadding = 0.0f; public EllipsizingTextView(Context context) {
this(context, null);
} public EllipsizingTextView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
} public EllipsizingTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
super.setEllipsize(null);
TypedArray a = context.obtainStyledAttributes(attrs, new int[] { android.R.attr.maxLines });
setMaxLines(a.getInt(0, Integer.MAX_VALUE));
} public void addEllipsizeListener(EllipsizeListener listener) {
if (listener == null) {
throw new NullPointerException();
}
ellipsizeListeners.add(listener);
} public void removeEllipsizeListener(EllipsizeListener listener) {
ellipsizeListeners.remove(listener);
} public boolean isEllipsized() {
return isEllipsized;
} @Override
public void setMaxLines(int maxLines) {
super.setMaxLines(maxLines);
this.maxLines = maxLines;
isStale = true;
} public int getMaxLines() {
return maxLines;
} public boolean ellipsizingLastFullyVisibleLine() {
return maxLines == Integer.MAX_VALUE;
} @Override
public void setLineSpacing(float add, float mult) {
this.lineAdditionalVerticalPadding = add;
this.lineSpacingMultiplier = mult;
super.setLineSpacing(add, mult);
} @Override
protected void onTextChanged(CharSequence text, int start, int before,
int after) {
super.onTextChanged(text, start, before, after);
if (!programmaticChange) {
fullText = text.toString();
isStale = true;
}
} @Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
if (ellipsizingLastFullyVisibleLine()) {
isStale = true;
}
} public void setPadding(int left, int top, int right, int bottom) {
super.setPadding(left, top, right, bottom);
if (ellipsizingLastFullyVisibleLine()) {
isStale = true;
}
} @Override
protected void onDraw(Canvas canvas) {
if (isStale) {
resetText();
}
super.onDraw(canvas);
} private void resetText() {
String workingText = null;
workingText = padString(fullText);
if (enableLog) Common.traceLog('d', TAG, "fullText= " + fullText.length() + " workingText= " + workingText.length()); boolean ellipsized = false;
Layout layout = createWorkingLayout(workingText);
int linesCount = getLinesCount();
if (enableLog) Common.traceLog('d', TAG, "layout.getLineCount()= " + layout.getLineCount() + " linesCount= " + linesCount); if (layout.getLineCount() > linesCount) {
// We have more lines of text than we are allowed to display.
// workingText = workingText.substring(0, layout.getLineEnd(linesCount - 1)).trim();
workingText = workingText.substring(0, layout.getLineEnd(linesCount - 1));
// if (enableLog) Common.traceLog('d', TAG, "workingText= " + workingText + " length= " + workingText.length()); while (createWorkingLayout(workingText + ELLIPSIS).getLineCount() > linesCount) {
// int lastSpace = workingText.lastIndexOf(' '); // Chinese has no ' ' as separator
// if (lastSpace == -1) {
// break;
// }
// workingText = workingText.substring(0, lastSpace);
// Chinese has no ' ' as separator, so truncate 1 by 1 and try
workingText = workingText.substring(0, workingText.length() - 1); 
// if (enableLog) Common.traceLog('d', TAG, "workingText= " + workingText);
}     workingText = workingText.substring(0, workingText.length() - 1); 
workingText = workingText + ELLIPSIS;
if (enableLog) Common.traceLog('d', TAG, "workingText= " + workingText);
ellipsized = true;
}
if (!workingText.equals(getText())) {
programmaticChange = true;
try {
setText(workingText);
} finally {
programmaticChange = false;
}
}
isStale = false;
if (ellipsized != isEllipsized) {
isEllipsized = ellipsized;
for (EllipsizeListener listener : ellipsizeListeners) {
listener.ellipsizeStateChanged(ellipsized);
}
}
} /**
 * Get how many lines of text we are allowed to display.
 */
private int getLinesCount() {
if (ellipsizingLastFullyVisibleLine()) {
int fullyVisibleLinesCount = getFullyVisibleLinesCount();
if (fullyVisibleLinesCount == -1) {
return 1;
} else {
return fullyVisibleLinesCount;
}
} else {
return maxLines;
}
} /**
 * Get how many lines of text we can display so their full height is visible.
 */
private int getFullyVisibleLinesCount() {
Layout layout = createWorkingLayout("");
int height = getHeight() - getPaddingTop() - getPaddingBottom();
int lineHeight = layout.getLineBottom(0);
// if (enableLog) Common.traceLog('d', TAG, "height= " + height + " lineHeight=" + lineHeight);
return height / lineHeight;
} private Layout createWorkingLayout(String workingText) {
StaticLayout s = new StaticLayout(workingText, getPaint(),
getWidth() - getPaddingLeft() - getPaddingRight(),
Alignment.ALIGN_NORMAL, lineSpacingMultiplier,
lineAdditionalVerticalPadding, false /* includepad */);
return s;
} @Override
public void setEllipsize(TruncateAt where) {
// Ellipsize settings are not respected
}

// pad the input string so it can be formed properly later on, hopefully
// android StaticLayout has difficulty in formating Chinese punctuation properly
private String padString(String inStr) {
int inLength = inStr.length();
char[] inChar = inStr.toCharArray();
char[] outChar = new char[inLength*3];
int outIndex = 0;
for(int i=0; i<inLength; i++ ) {
char a = inChar[i]; switch (a) {
case ',':
outChar[outIndex++] = ',';
outChar[outIndex++] = 0x20;
break;
case '。':
outChar[outIndex++] = 0x20;
outChar[outIndex++] = a;
break;
case '?':
outChar[outIndex++] = '?';
outChar[outIndex++] = 0x20;
break;
case '!':
outChar[outIndex++] = '!';
outChar[outIndex++] = 0x20;
break;
case ':':
outChar[outIndex++] = ':';
outChar[outIndex++] = 0x20;
break;
case '丶':
outChar[outIndex++] = 0x20;
outChar[outIndex++] = a;
break;
case '“':
outChar[outIndex++] = '"';
outChar[outIndex++] = 0x20;
break;
case '”':
outChar[outIndex++] = '"';
outChar[outIndex++] = 0x20;
break;
case ';':
outChar[outIndex++] = ';';
outChar[outIndex++] = 0x20;
break;
case (char)0xefbda1:
outChar[outIndex++] = 0x20;
outChar[outIndex++] = a;
break;
default:
outChar[outIndex++] = a;
break;
}
// if( a ==',' 
// || a == '。'
// || a == '?' 
// || a == '!' 
// || a == ':' 
// || a == '丶' 
// || a == '“' 
// || a == '”' 
// || a == ';' // UTF-8 0xefbc9b
// || a == 0xefbda1) {
//// outChar[outIndex++] = 0x20;
// outChar[outIndex++] = a;
// outChar[outIndex++] = 0x20;
// }
// else {
// outChar[outIndex++] = a;
// }
}
// if (enableLog) Common.traceLog('d', TAG, "outIndex= " + outIndex);
String result = new String (outChar, 0, outIndex);
return result;
}
}本人菜鸟,初入贵坛,还请赐教