【问题标题】:Changing Color Of TextView text in android using spannableString使用 spannableString 在 android 中更改 TextView 文本的颜色
【发布时间】:2016-09-02 22:22:31
【问题描述】:

我有一个字符串

1 Friend | O Reviews | 0 Coupons

我正在使用以下代码

SpannableString hashText = new SpannableString(TextUtils.concat(
                friendsText,
                " | ",
                reviewsText,
                " | ",
                couponsText
        ).toString());

        Matcher matcher = Pattern.compile("\\s* | \\s*").matcher(hashText);
        while (matcher.find())
        {
            hashText.setSpan(new ForegroundColorSpan(getResources().getColor(R.color.blue)), matcher.start(), matcher.end(), 0);
        }

        detailsText.setText(hashText);

我想改变 | 的颜色从 TextView 原始灰色到蓝色。 上面的代码什么也没做。我做错了什么。

【问题讨论】:

    标签: java android eclipse android-studio


    【解决方案1】:

    这样试试

    String text = TextUtils.join(" | ", Arrays.asList(new String[]{"1 Friend", "1 Review", "1 Coupons"}));
    
            Spannable spannable = new SpannableString(text);
    
            int index = text.indexOf('|');
            while (index >= 0) {
                spannable.setSpan(new ForegroundColorSpan(Color.BLUE), index, index + 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                index = text.indexOf('|', index + 1);
            }
            detailsText.setText(hashText);
    

    输出:

    【讨论】:

    • " | " 被放置在多个地方,它确实适用于 1 |但对于字符串中的任何地方
    • @MuhammadUmar 它适用于找到“|”的任何地方。请运行并查看输出。我添加了相同的屏幕截图。
    • 可以看到一个很好的跨度示例here
    【解决方案2】:

    试试这个,

    Spannable spannable = new SpannableString(text);
    
    int index = text.indexOf(“|”);
    
    while ( index >= 0) {
        spannable.setSpan(new ForegroundColorSpan(Color.BLUE), index, index + 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        index = text.indexOf(“|”, index + 1);
    }
    

    【讨论】:

      【解决方案3】:

      即使这里是公认的答案,但我想为用户分享最简单的方法。

      public void setHighLightedText(TextView tv, String textToHighlight) {
          String tvt = tv.getText().toString();
          int ofe = tvt.indexOf(textToHighlight, 0);
          Spannable wordToSpan = new SpannableString(tv.getText());
      
          for (int ofs = 0; ofs < tvt.length() && ofe != -1; ofs = ofe + 1) {
              ofe = tvt.indexOf(textToHighlight, ofs);
              if (ofe == -1)
                  break;
              else {
                  // you can change or add more span as per your need
                  wordToSpan.setSpan(new RelativeSizeSpan(2f), ofe,ofe + textToHighlight.length(), 0); // set size
                  wordToSpan.setSpan(new ForegroundColorSpan(Color.RED), ofe, ofe + textToHighlight.length(), 0);// set color
                  tv.setText(wordToSpan, TextView.BufferType.SPANNABLE);
              }
          }
      }
      

      像这样调用这个方法

      textView.setText("1 Friend | O Reviews | 0 Coupons");
      setHighLightedText(textView,"|");
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-04-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多