【问题标题】:How to set color for string in android如何在android中为字符串设置颜色
【发布时间】:2019-03-27 08:17:57
【问题描述】:

我有一个webview。我只是在string 中检索webview's 内容并将其显示在textview 中。在onTouch 事件中,文本将大声朗读。现在我想在文本正在讲话的onTouch 事件中逐字突出显示文本。对于textview,我们可以使用setTextColor。但是如何setTextColor 获取字符串呢?

【问题讨论】:

    标签: android string textview


    【解决方案1】:

    使用SpannableString,例如:

    final String s = editText.getText().toString();
    final Spannable spannable = new SpannableString(s);
    spannable.setSpan(new ForegroundColorSpan(Color.RED), 0, endIndex, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    

    更新endIndex 以指向正在说的任何单词的结尾,

    【讨论】:

      【解决方案2】:

      也可以这样使用

      String name = "Test String"; 
      textView.setText( Html.fromHtml(String.format("<font color=\"#%s\">text</font>", name ), TextView.BufferType.SPANNABLE));
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-12-03
        • 1970-01-01
        相关资源
        最近更新 更多