android给TextView设置部分文本颜色

  1. import android.graphics.Color;
  2. import android.os.Bundle;
  3. import android.support.annotation.Nullable;
  4. import android.support.v7.app.AppCompatActivity;
  5.  import android.view.Gravity;
  6.  import android.widget.LinearLayout;
  7.  import android.widget.TextView;
  8.   
  9.  public class MainActivity extends AppCompatActivity {
  10.   
  11. @Override
  12.  protected void onCreate(@Nullable Bundle savedInstanceState) {
  13.  super.onCreate(savedInstanceState);
  14.  setContentView(R.layout.activity_main);
  15.   
  16.  LinearLayout llContent = findViewById(R.id.ll_content);
  17.  for (int i = 0; i < 6; i++) {
  18.  TextView textView = new TextView(this);
  19.  textView.setTextSize(14);
  20.  textView.setGravity(Gravity.CENTER);
  21.  textView.setTextColor(Color.WHITE);
  22.  textView.setPadding(0, 20, 0, 20);
  23.  setText(textView, i);
  24.  llContent.addView(textView);
  25.  }
  26.  }
  27.  
     
  28.  
    /**
  29.  
    * 方法描述:设置文本内容
  30.  
    **/
  31.  private void setText(TextView textView, int position) {
  32.  String text = "注意:打雷下雨了,快回家收衣服吧!";
  33.  switch (position) {
  34.  case 0:
  35.  // 根据文本下标,指定单个部分文字变色
  36.  textView.setText(StringDesignUtil.getSpannableStringBuilder(text, Color.RED, 3, 5));
  37.  break;
  38.  case 1:
  39.  // 指定关键字变色,并且给相对应的关键指定颜色
  40.  textView.setText(StringDesignUtil.getSpannableStringBuilder(text, new String[]{"打雷", "收衣服"}, new int[]{Color.BLUE, Color.RED}));
  41.  break;
  42.  case 2:
  43.  // 文本由关键字拼接而成,文本内容与字体颜色一一对应显示
  44.  textView.setText(StringDesignUtil.getSpannableStringBuilder(
  45.  new String[]{"注意:", "打雷", "下雨了,快回家", "收衣服", "吧!"},
  46.  new int[]{Color.WHITE, Color.RED, Color.WHITE, Color.GREEN, Color.WHITE}));
  47.  break;
  48.  
  49. case 3:
  50.  // 根据关键字,指定单个部分文字颜色
  51.  textView.setText(StringDesignUtil.getSpanned(text, "打雷", "#e607bf"));
  52.  break;
  53.  case 4:
  54.  // 指定关键字变色,并且给相对应的关键指定颜色
  55.  textView.setText(StringDesignUtil.getSpanned(text,
  56.  new String[]{"打雷", "收衣服"},
  57.  new String[]{"#ff0000", "#185af0"}));
  58.  break;
  59.  case 5:
  60.  // 文本由关键字拼接而成,文本内容与字体颜色一一对应显示
  61.  textView.setText(StringDesignUtil.getSpanned(
  62.  new String[]{"注意:", "打雷", "下雨了,快回家", "收衣服", "吧!"},
  63.  new String[]{"#ffffff", "#ff0000", "#ffffff", "#185af0", "#ffffff",}));
  64.  break;
  65.  }
  66.  }
  67.   
  68.  
  69.  
  70.  import android.text.Html;
  71.  import android.text.Spannable;
  72.  import android.text.SpannableStringBuilder;
  73.  import android.text.Spanned;
  74.  import android.text.TextUtils;
  75.  import android.text.style.ForegroundColorSpan;
  76.   
  77. /**
  78.  * 类描述:字符串设计工具类
  79.  */
  80.  public class StringDesignUtil {
  81.    /**
  82.  * 方法描述:根据文本下标,指定单个部分文字变色
  83.  **/
  84.  public static SpannableStringBuilder getSpannableStringBuilder(String text, int color, int startIndex, int entIndex) {
  85.  SpannableStringBuilder builder = new SpannableStringBuilder(text);
  86.  if (startIndex >= 0 && entIndex > startIndex && entIndex <= text.length()) {
  87.  builder.setSpan(new ForegroundColorSpan(color), startIndex, entIndex, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
  88.  }
  89.  return builder;
  90.  }
  91.  
     
  92.  /**
  93.  * 方法描述:指定关键字变色,并且给相对应的关键指定颜色
  94.  **/
  95.  public static SpannableStringBuilder getSpannableStringBuilder(String text, String texts[], int color[]) {
  96.  SpannableStringBuilder builder = new SpannableStringBuilder(text);
  97.  if (texts != null) {
  98.  for (int i = 0; i < texts.length; i++) {
  99.  String value = texts[i];
  100.  if (!TextUtils.isEmpty(value) && text.contains(value)) {
  101.  if (color != null && color.length > i) {
  102.  int startIndex = text.indexOf(value);
  103.  int entIndex = startIndex + value.length();
  104.  if (entIndex > startIndex && entIndex <= text.length()) {
  105.  builder.setSpan(new ForegroundColorSpan(color[i]), startIndex, entIndex, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
  106.  }
  107.  }
  108.  }
  109.  }
  110.  }
  111.  return builder;
  112.  }
  113.   
  114.  /**
  115.  * 方法描述: 文本由关键字拼接而成,文本内容与字体颜色一一对应显示
  116.  **/
  117.  public static SpannableStringBuilder getSpannableStringBuilder(String text[], int color[]) {
  118.  SpannableStringBuilder builder = new SpannableStringBuilder();
  119.  if (text != null) {
  120.  for (int i = 0; i < text.length; i++) {
  121.  int startIndex = builder.length();
  122.  builder.append(text[i]);
  123.  if (color != null && color.length > i) {
  124.  builder.setSpan(new ForegroundColorSpan(color[i]), startIndex, builder.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
  125.  }
  126.  }
  127.  }
  128.  return builder;
  129.  }
  130.   
  131.  /**
  132.  * 根据关键字,指定单个部分文字颜色
  133.  **/
  134.  public static Spanned getSpanned(String text, String keyword, String colorValue) {
  135.  return Html.fromHtml(text.replace(keyword, "<font color=" + colorValue + ">" + keyword + "</font>"));
  136.  }
  137.  
     
  138.  /**
  139.  * 指定关键字变色,并且给相对应的关键指定颜色
  140.  **/
  141.  public static Spanned getSpanned(String text, String keyword[], String colorValue[]) {
  142.  if (keyword != null && colorValue != null) {
  143.  for (int i = 0; i < keyword.length; i++) {
  144.  if (colorValue.length > i) {
  145.  text = text.replace(keyword[i], "<font color=" + colorValue[i] + ">" + keyword[i] + "</font>");
  146.  }
  147.  }
  148.  }
  149.  return Html.fromHtml(text);
  150.  }
  151.   
  152.  /** 
  153. * 文本由关键字拼接而成,文本内容与字体颜色一一对应显示
  154.  **/
  155.  public static Spanned getSpanned(String keyword[], String colorValue[]) {
  156.  StringBuffer buffer = new StringBuffer();
  157.  if (keyword != null && colorValue != null) {
  158. for (int i = 0; i < keyword.length; i++) {
  159.  if (colorValue.length > i) {
  160.  buffer.append("<font color=" + colorValue[i] + ">" + keyword[i] + "</font>");
  161.  }
  162.  }
  163.  }
  164.  return Html.fromHtml(buffer.toString());
  165.  }
  166.  
  167.  }

运行效果图:

原文地址:https://www.cnblogs.com/Alex80/p/14042411.html