The effect is as follows:
/**
*@paramTextView
*@paramPositionsThe position of discoloration
*@paramColorsMultiple color values, number and number of positions can be inconsistent, if the color value is less than the position value, it is used to cross the
*@ descriptionSet a textivew to display multiple colors
*@ AuthorTll
*@ Time2016/11/11 10:37
*/
public static voidSetmultiplecolor (TextView TextView, list<integer> positions, Integer ... colors) {
if (IsEmpty (positions)) {
Return
}
Spannablestringbuilder Builder =New Spannablestringbuilder (Textview.gettext (). toString ());
for (int i =0; I < Positions.size (); i++) {
Foregroundcolorspan span =New Foregroundcolorspan (colors[i% colors.Length]);
if (i = =0) {
Builder.setspan (span, 0, Positions.get (i), spannable. span_exclusive_exclusive);
} Else {
Builder.setspan (span, positions.get (i- 1), Positions.get (i), spannable. span_exclusive_exclusive);
}
}
Textview.settext (builder);
}
How to use:
Text = (TextView) Findviewbyid (r.id.Text;
list<integer> positions =New Arraylist<> ();
Positions.add (4);
Positions.add (6);
Positions.add (11);
Positions.add (13);
Positions.add (Text.gettext (). toString (). Length ());
Setmultiplecolor (Text, positions,< Span style= "COLOR: #6897bb" > < Span style= "COLOR: #cc7832" > getresources (). getcolor (R.color. color_353535) getresources (). GetColor (R.color.color_f67067)"
< Span style= "COLOR: #cc7832" > < Span style= "COLOR: #6897bb" >< Span style= "COLOR: #cc7832" > &NBSP;
Android sets multiple font colors for TextView