android 无法使用 html 设置特殊颜色

android can't set special color using html

在字符串文件中:

<string name="test1">AAAA<font color="#ff0000">BBBB</font></string>
<string name="test2">AAAA<font color="red">BBBB</font></string>

在布局文件中,我这样设置了一个TextView:

android:text="@string/test1"

但是没有用!! "BBBB" 不转为“#ff0000”。

当我像这样设置 TextView 时:

android:text="@string/test2"

它works.the"BBBB"变成红色

我想知道如何设置我想要的颜色的文本; 不只是红蓝绿...

对 ex:

使用 SpannableString
SpannableString ss = new SpannableString("hi #xyz how are you.");
ss.setSpan(new ForegroundColorSpan(Color.RED), 3, 8, 0);

打开 res 目录,然后打开值。在那里创建新的 Value resource file。 并将其命名为您想要的名称,例如:color

现在打开color.xml并像这样编辑

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="color1">#3254f1</color>
    <color name="color2">#265646</color>
</resources>

现在将此行放入您的 TextView 标签中

android:textColor="@color/color1"