这里有新鲜出炉的精品教程,程序狗速度看过来!
Android 是一种基于 Linux 的自由及开放源代码的操作系统,主要使用于移动设备,如智能手机和平板电脑,由 Google 公司和开放手机联盟领导及开发。尚未有统一中文名称,中国大陆地区较多人使用 "安卓" 或 "安致"。
这篇文章主要介绍了 Android 实现不同字体颜色的 TextView 实现代码的相关资料, 需要的朋友可以参考下
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,不同颜色。
来源: http://www.phperz.com/article/17/0821/337617.html