Android 實(shí)現(xiàn)不同字體顏色的TextView
遇到的需求,mark一下。
實(shí)現(xiàn)代碼:
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;/** * 項(xiàng)目名稱:trunk * 類描述: * 創(chuàng)建人:Administrator * 創(chuàng)建時(shí)間:2015/12/10 14:05 * 修改人:Administrator * 修改時(shí)間: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; }}實(shí)現(xiàn)同一textView,不同顏色。
感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注