import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.ForegroundColorSpan;
import android.widget.TextView;

/**
* 目前是在一个textview中显示两种不同颜色的内容,满足目前需求,暂不改成更通用的方式
*
* @author wangll
*
*/

public class TextViewUtil {

public static void setTextViewColor(int fristColor, int secondColor, TextView textView, String nickName, String content) {
int nameLength = nickName.length();
SpannableStringBuilder builder = new SpannableStringBuilder(nickName + content);
// ForegroundColorSpan 为文字前景色,BackgroundColorSpan为文字背景色
ForegroundColorSpan redSpan = new ForegroundColorSpan(fristColor);
ForegroundColorSpan whiteSpan = new ForegroundColorSpan(secondColor);
builder.setSpan(redSpan, 0, nameLength, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
builder.setSpan(whiteSpan, nameLength, nameLength+content.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);
textView.setText(builder);
}

public static void setTextViewColor(int fristColor, int secondColor, int thirdColor, TextView textView, String fristText, String nickName, String content) {
int fristLength = fristText.length();
int nickNameLength = nickName.length();

SpannableStringBuilder builder = new SpannableStringBuilder(fristText+nickName + content);
// ForegroundColorSpan 为文字前景色,BackgroundColorSpan为文字背景色
ForegroundColorSpan redSpan = new ForegroundColorSpan(fristColor);
ForegroundColorSpan whiteSpan = new ForegroundColorSpan(secondColor);
ForegroundColorSpan thirdSpan = new ForegroundColorSpan(thirdColor);
builder.setSpan(redSpan, 0, fristLength, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
builder.setSpan(whiteSpan, fristLength, nickNameLength+fristLength, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
builder.setSpan(thirdSpan, nickNameLength+fristLength, nickNameLength+fristLength+content.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);
textView.setText(builder);

}

}

方式二.

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.support.annotation.Nullable;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.TextUtils;
import android.text.style.ForegroundColorSpan;
import android.util.AttributeSet;

import cn.mvp.mlibs.R;


/**

* Android自定义TextView实现必填项前面的*号
*/
public class RequiredTextView extends android.support.v7.widget.AppCompatTextView {

private String prefix = "*";
private int prefixColor = Color.RED;

public RequiredTextView(Context context) {
super(context);
}

public RequiredTextView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
init(context, attrs);
}

public RequiredTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context, attrs);
}

private void init(Context context, @Nullable AttributeSet attrs) {
TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.RequiredTextView);

prefix = ta.getString(R.styleable.RequiredTextView_prefix);
prefixColor = ta.getInteger(R.styleable.RequiredTextView_prefix_color, Color.RED);
String text = ta.getString(R.styleable.RequiredTextView_android_text);
if (TextUtils.isEmpty(prefix)) {
prefix = "*";
}
if (TextUtils.isEmpty(text)) {
text = "";
}
ta.recycle();
setText(text);
}

public void setText(String text) {
Spannable span = new SpannableString(prefix + text);
span.setSpan(new ForegroundColorSpan(prefixColor), 0, prefix.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
setText(span);
}

}
<declare-styleable name="RequiredTextView">
<attr name="android:text" />
<attr name="prefix" format="string|reference" />
<attr name="prefix_color" format="color|reference" />
</declare-styleable>