【问题标题】:Android Color specific words in an ArrayList<String> [duplicate]ArrayList<String>中的Android颜色特定单词[重复]
【发布时间】:2013-12-08 11:23:02
【问题描述】:

我有一个ArrayList&lt;String&gt;,里面有很多词。我想在里面给特定的词上色,这可能吗?我怎样才能做到这一点?

ex. 我的ArrayList 中有这个:

我喜欢法拉利汽车,因为它们#Awesome!

我怎样才能让 Awesome 变成不同的颜色?以及如何为标点符号(和!)着色?

【问题讨论】:

    标签: java android arraylist


    【解决方案1】:

    试试这个..

    第一个是

        SpannableString WordtoSpan = new SpannableString("This is simple");        
        WordtoSpan.setSpan(new ForegroundColorSpan(Color.BLUE), 0, 7, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    
    //0 is starting character and 7 is ending character you need to specify it
    
    
      textView.setText(WordtoSpan);
    

    第二个是

    String styledText = "This is <font color='red'>simple</font>.";
    textView.setText(Html.fromHtml(styledText), TextView.BufferType.SPANNABLE);
    

    编辑:

    String txt = "partial colored #text";
            int to_len = txt.length();
            int sim_pos = txt.indexOf("#");
    
            SpannableString WordtoSpan = new SpannableString(txt);        
              WordtoSpan.setSpan(new ForegroundColorSpan(Color.BLUE), sim_pos, to_len, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
              textView.setText(WordtoSpan);
    

    【讨论】:

    • 我听不懂,你能指导我更多关于如何使用 SpannableString 和我的 arraylist 来检测 # 或标点符号吗?
    • @JohnJared 不,它不会检测到 # 或标点符号。只需从起始字符位置到结束字符位置即可。
    • 谢谢,0-7 的字符已着色。但这不是我要求的,请重新阅读问题
    • @JohnJared 检查我的编辑。
    【解决方案2】:

    您可以使用可扩展字符串,或者如果您使用 HTML,您可以从 HTML 文本加载文本视图。您可以在here 上查看示例

    【讨论】:

    • 你能解释一下吗?
    • 如果要出现#,你可以给出 element.indexof("#") 和 end element ad length of element
    猜你喜欢
    • 2016-06-13
    • 2022-07-05
    • 1970-01-01
    • 2013-06-20
    • 1970-01-01
    • 1970-01-01
    • 2016-08-29
    • 2021-10-13
    • 1970-01-01
    相关资源
    最近更新 更多