遇到的需求,mark一下:
package com.chuck.recyclerviewdemo;
import android.content.Context;
import android.graphics.Canvas;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.util.AttributeSet;
import android.widget.TextView;
import java.util.List;
/**
* 項目名稱:trunk
* 類描述:
* 創建人:Administrator
* 創建時間:2015/12/10 14:05
* 修改人:Administrator
* 修改時間:2015/12/10 14:05
* 修改備註:
*/
public class DifferentColorTextView extends TextView{
public DifferentColorTextView(Context context) {
super(context);
}
public DifferentColorTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public DifferentColorTextView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
}
public void setDifferentColorText(List<String> text,List<Integer>colors){
setText(calculateResidue(text,colors));
}
/**顯示不同顏色*/
private SpannableStringBuilder calculateResidue(List<String> text, List<Integer>colors) {
if(text==null||colors==null){
return null;
}
StringBuilder sb =new StringBuilder();
for (int i=0;i<text.size();i++){
sb.append(text.get(i));
}
SpannableStringBuilder ssb=new SpannableStringBuilder(sb.toString());
int begin=0;
for (int i=0;i<text.size();i++){
ForegroundColorSpan mSpan = new ForegroundColorSpan(colors.get(i));
ssb.setSpan(mSpan,begin,begin+text.get(i).length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
begin=begin+text.get(i).length();
}
return ssb;
}
}
實現同一textView,不同顏色。