Android 實現(xiàn)不同字體顏色的TextView實現(xiàn)代碼
更新時間:2017年05月26日 17:23:39 作者:ChuckChenLw
這篇文章主要介紹了Android 實現(xiàn)不同字體顏色的TextView實現(xiàn)代碼的相關(guān)資料,需要的朋友可以參考下
Android 實現(xiàn)不同字體顏色的TextView
遇到的需求,mark一下。
實現(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; /** * 項目名稱:trunk * 類描述: * 創(chuàng)建人:Administrator * 創(chuàng)建時間: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; } }
實現(xiàn)同一textView,不同顏色。
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
相關(guān)文章
Android?Studio打包?aar實現(xiàn)步驟示例詳解
這篇文章主要為大家介紹了Android?Studio打包aar步驟示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-08-08詳解Android開發(fā)數(shù)據(jù)持久化之文件存儲(附源碼)
本篇文章主要介紹了詳解Android開發(fā)數(shù)據(jù)持久化之文件存儲(附源碼),具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-03-03詳解Android:向服務器提供數(shù)據(jù)之get、post方式
本篇文章主要介紹了詳解Android:向服務器提供數(shù)據(jù)之get、post方式,具有一定的參考價值,有興趣的可以了解一下。2017-03-03淺扒Android動態(tài)設(shè)置字體大小的示例
本篇文章主要介紹了淺扒Android動態(tài)設(shè)置字體大小的示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-12-12