gpt4 book ai didi

java - Android 为 ArrayList 中的特定单词着色

转载 作者:行者123 更新时间:2023-12-01 18:40:22 26 4
gpt4 key购买 nike

我有一个ArrayList<String>那有很多字。我想为其中的特定单词着色,可以吗?我怎样才能实现这一目标?

例如。我的 ArrayList 中有这个:

I love ferrari cars, because they are #Awesome!

如何使 Awesome 具有不同的颜色?以及如何为标点符号(和!)着色?

最佳答案

试试这个..

第一个是

    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);

关于java - Android 为 ArrayList<String> 中的特定单词着色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20164191/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com