Android TextView两端对齐
先看看效果:
![](https://i-blog.csdnimg.cn/blog_migrate/6a074414cd633a158ff0a31a06c0dd4d.png)
![](https://i-blog.csdnimg.cn/blog_migrate/03f9dd3ef857a0f0355b09cf79a5e3ff.png)
public class BothEndsBothEndsAlignTextView extends AppCompatTextView {
private final String TAG = BothEndsAlignTextView.class.getSimpleName();
private boolean alignOnlyOneLine;
public BothEndsBothEndsAlignTextView(Context context) {
this(context, null);
}
public BothEndsBothEndsAlignTextView(Context context, @Nullable AttributeSet attrs) {
this(context, attrs, 0);
}
public BothEndsBothEndsAlignTextView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context, attrs);
}
private void init(Context context, AttributeSet attrs) {
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.BothEndsAlignTextView);
alignOnlyOneLine = typedArray.getBoolean(R.styleable.BothEndsAlignTextView_alignOnlyOneLine, false);
ColorStateList colorStateList = typedArray.getColorStateList(R.styleable.BothEndsAlignTextView_android_textColor);
setTextColor(colorStateList==null?ColorStateList.valueOf(Color.GRAY):colorStateList);
typedArray.recycle();
}
@Override
public void setTextColor(int color) {
super.setTextColor(color);
getPaint().setColor(color);
}
@Override
public void setTextColor(ColorStateList colors) {
super.setTextColor(colors);
getPaint().setColor(colors.getDefaultColor());
}
protected void onDraw(Canvas canvas) {
CharSequence content = getText();
if (content instanceof String){
String text = (String) content;
Layout layout = getLayout();
for (int i = 0; i < layout.getLineCount(); ++i) {
int lineBaseline = layout.getLineBaseline(i) + getPaddingTop();
int lineStart = layout.getLineStart(i);
int lineEnd = layout.getLineEnd(i);
if (alignOnlyOneLine && layout.getLineCount() == 1) {//只有一行
String line = text.substring(lineStart, lineEnd);
float width = StaticLayout.getDesiredWidth(text, lineStart, lineEnd, getPaint());
this.drawScaledText(canvas, line, lineBaseline, width);
} else if (i == layout.getLineCount() - 1) {//最后一行
canvas.drawText(text.substring(lineStart), getPaddingLeft(), lineBaseline, getPaint());
break;
} else {//中间行
String line = text.substring(lineStart, lineEnd);
float width = StaticLayout.getDesiredWidth(text, lineStart, lineEnd, getPaint());
this.drawScaledText(canvas, line, lineBaseline, width);
}
}
}else if (content instanceof Spanned){
Spanned text = (Spanned) content;
Layout layout = getLayout();
for (int i = 0; i < layout.getLineCount(); ++i) {
int lineBaseline = layout.getLineBaseline(i) + getPaddingTop();
int lineStart = layout.getLineStart(i);
int lineEnd = layout.getLineEnd(i);
ImageSpan[] imageSpans = text.getSpans(lineStart, lineEnd, ImageSpan.class);
if (imageSpans.length > 0){//只适用于一张图片一行
imageSpans[0].draw(canvas,text,lineStart,lineEnd,getPaddingLeft(),0,0,layout.getLineBaseline(i),getSpannedPaint(text,lineStart));
}else if (alignOnlyOneLine && layout.getLineCount() == 1) {//只有一行
float width = StaticLayout.getDesiredWidth(text, lineStart, lineEnd, getPaint());
this.drawScaledText(canvas, text, lineStart, lineEnd, lineBaseline, width, true);
} else if (i == layout.getLineCount() - 1) {//最后一行
//canvas.drawText(text, lineStart, text.length(), getPaddingLeft(), lineBaseline, getPaint());
float width = StaticLayout.getDesiredWidth(text, lineStart, lineEnd, getPaint());
this.drawScaledText(canvas, text, lineStart, lineEnd, lineBaseline, width,false);
break;
} else {//中间行
float width = StaticLayout.getDesiredWidth(text, lineStart, lineEnd, getPaint());
boolean forceNextLine = text.charAt(lineEnd-1) == 10;
if (forceNextLine){
this.drawScaledText(canvas, text, lineStart, lineEnd, lineBaseline, width,false);
}else {
this.drawScaledText(canvas, text, lineStart, lineEnd, lineBaseline, width, true);
}
}
}
}else {
super.onDraw(canvas);
}
}
private void drawScaledText(Canvas canvas, String line, float baseLineY, float lineWidth) {
if (line.length() < 1) {
return;
}
float x = getPaddingLeft();
boolean forceNextLine = line.charAt(line.length() - 1) == 10;
int length = line.length() - 1;
if (forceNextLine || length == 0) {
canvas.drawText(line, x, baseLineY, getPaint());
return;
}
float d = (getMeasuredWidth() - lineWidth - getPaddingLeft() - getPaddingRight()) / length;
for (int i = 0; i < line.length(); ++i) {
String c = String.valueOf(line.charAt(i));
float cw = StaticLayout.getDesiredWidth(c, getPaint());
canvas.drawText(c, x, baseLineY, this.getPaint());
x += cw + d;
}
}
private void drawScaledText(Canvas canvas, Spanned text, int start, int end, float baseLineY, float lineWidth, boolean isAlign) {
if ((end - start) < 0) {
return;
}
float x = getPaddingLeft();
int length = end - start;
if (length == 0){
canvas.drawText(text, start, end, x, baseLineY, getSpannedPaint(text,start));
return;
}
float d;//每个字符间需要添加的间隔
if (isAlign){
d = (getMeasuredWidth() - lineWidth - getPaddingLeft() - getPaddingRight()) / length;
}else {
d = 0;
}
for (int i = 0; i < length; ++i) {
float cw = StaticLayout.getDesiredWidth(text,start + i,start + i+1, getSpannedPaint(text,start + i));
canvas.drawText(text, start + i,start + i + 1, x, baseLineY, getSpannedPaint(text,start + i));
x += cw + d;
}
}
/**
* 获取单个字符的式样
* @param text Spanned
* @param index 字符的索引
* @return TextPaint
*/
private TextPaint getSpannedPaint(Spanned text, int index){
TextPaint textPaint = new TextPaint();
textPaint.set(getPaint());
CharacterStyle[] characterSpans = text.getSpans(index, index+1, CharacterStyle.class);
if (characterSpans.length > 0){
for (CharacterStyle span:characterSpans){
span.updateDrawState(textPaint);
}
}
return textPaint;
}
}
attrs.xml
<!-- 两端对齐方式 -->
<declare-styleable name="BothEndsBothEndsAlignTextView">
<!-- 只有一行是是否两端对齐 -->
<attr name="alignOnlyOneLine" format="boolean"/>
<!-- 字体颜色 -->
<attr name="android_textColor" format="color"/>
</declare-styleable>
直接用:
<com.xx.widget.BothEndsBothEndsAlignTextView
android:id="@+id/tv_job"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_marginRight="16dp"
android:text=""
android:textStyle="bold"
android:textColor="@color/text_gray_deep"
android:textSize="15dp" />