国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android 實現不同字體顏色的TextView實現代碼

2019-12-12 02:48:05
字體:
來源:轉載
供稿:網友

Android 實現不同字體顏色的TextView

遇到的需求,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,不同顏色。

        感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 土默特左旗| 西贡区| 深圳市| 福安市| 徐闻县| 当雄县| 双城市| 东源县| 长宁县| 大渡口区| 峡江县| 永济市| 浮梁县| 剑河县| 富裕县| 囊谦县| 海兴县| 通州市| 郯城县| 隆安县| 罗山县| 汉寿县| 鹰潭市| 青河县| 常德市| 古丈县| 博湖县| 丹寨县| 德兴市| 宁武县| 湘乡市| 建平县| 武汉市| 赤壁市| 民丰县| 甘泉县| 临邑县| 泰州市| 自治县| 柘城县| 通化县|