Android:无法更改textview中部分文本的颜色

标签 android html textview spannable

您好,我正在尝试将 TextView 中的部分文本更改为某种颜色,但我无法实现它 我尝试了 fromHtmlspannable 和不同的方法,但它仍然不起作用。

我的尝试如下:

尝试1

    Spannable wordtoSpan = Spannable.Factory.getInstance().newSpannable("text 6 Months(180 days) or 8,000 kilometers.");

    wordtoSpan.setSpan(new ForegroundColorSpan(Color.BLUE), 5, 15, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

    tv.setText(wordtoSpan);

尝试2

    String str = "@Second @Service:6 Months(180 days) or 8,000 kilometers.";

    String[] str_array = str .split(" ");

    boolean isExists = false;
    for (int i = 0; i < str_array.length; i++) {
             for (int j = 0; j < i; j++) {
                 if (str_array[j].equals(str_array[i])) {
                     isExists = true;
                 }
             }
             if (str_array[i].startsWith("@") && !isExists) {
                 str  = str .replace(str_array[i],
                         "<font color='#8A0A0A'>" + str_array[i]
                                 + "</font>");
             } else if (str_array[i].contains("#")) {
                 str  = str .replaceAll(str_array[i],
                         "<font color='#000000'><b>" + str_array[i]
                                 + "</b></font>");
             }
         }

         tv.setText(Html.fromHtml(str));
    }

尝试3

myTextView.setText(Html.fromHtml(text + "<font color=white>" + CepVizyon.getPhoneCode() + "</font><br><br>"
            + getText(R.string.currentversion) + CepVizyon.getLicenseText()));

尝试4

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //method1
        TextView tv = (TextView) findViewById(R.id.service);
        tv.setText(Html.fromHtml("<font color=\"#FFFFFF\">" + "give your string here long long longlong" + "</font>" + "<font color=\"#47a842\"></font>"));


        //method 2
        //SpannableStringBuilder WordtoSpan = new SpannableStringBuilder("give your string here long long longlong");
        //WordtoSpan.setSpan(new BackgroundColorSpan(Color.YELLOW),0,5,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        //tv.setText(WordtoSpan);

    }

非常感谢任何帮助。谢谢

最佳答案

尝试这样。下面的代码非常适合我,我已经测试过了。

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

这是输出。

enter image description here

关于Android:无法更改textview中部分文本的颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22041016/

相关文章:

android - Soundpool 频繁出现显着延迟并发出 "deep-buffer-playback"消息

android - 显示一小部分数字,一个在另一个之上,就像在 Libreoffice 上一样

swift - 根据是否是第一响应者设置 TextView 的背景颜色

javascript转义字符串中的换行符

android - 如何在具有多种字体大小的 TextView 中调整行高?

java - android TextWatcher 改变外部类私有(private)字段

android - 如何禁用 onClickListener 中的粘贴以获取 EditText Android 的 Drawable 权限(内部图标 EditText)

android - SQLite 数据库中的并发性

c# - ASP.NET:更改隐藏代码中的 CSS 链接 href?

javascript - 在焦点上打开一个 html 选择选项