android - 在 Android 中使用 Html.fromHtml 将颜色设置为 TextView 不起作用

标签 android html textview

我正在开发一个 Android 应用程序。在我的应用程序中,我试图为 TextView 中的文本设置不同的颜色。我的意思是 TextView 中的多种颜色。我正在尝试使用 Html.fromHtml 来做到这一点。但它不起作用。

TextView xml:

<TextView
    android:paddingTop="@dimen/general_line_spacing"
    android:paddingBottom="@dimen/general_line_spacing"
    android:textSize="@dimen/mm_item_title_size"
    android:textColor="@color/colorPrimaryText"
    android:id="@+id/mm_item_tv_title"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

这就是我将文本设置为 TextView

的方式
String title = post.getTitle();
if(title!=null && title.length()>MAX_TITLE_LENGTH)
{
    title = title.substring(0, MAX_TITLE_LENGTH);
    title = title + "<font color='color:#2bb1ff'> .... read more</font>";
}
viewHolder.tvTitle.setText(Html.fromHtml(title));

如您所见,我正在使用 html 设置字体颜色。但它不起作用。附加的“阅读更多”文本总是与其他字母颜色相同。所以我也尝试了这种方式。

title = title + "<![CDATA[<font color='color:#2bb1ff'> .... read more</font>]]>";

它不起作用。这也是:

title = title + "<span style=color:'#2bb1ff'> .... read more</span>";

那么如何在 TextView 中为文本设置多种颜色?

最佳答案

试试这个

title = title + "<font color=#2bb1ff> .... read more</font>";

关于android - 在 Android 中使用 Html.fromHtml 将颜色设置为 TextView 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41218724/

相关文章:

android - 我如何在 android 中以新更改的语言显示 toast 和标题

html - Firefox document.execCommand ('insertText' , ...)

html - TypeScript:ev.target 和 Node.contains:EventTarget 不可分配给 Node

android - HoloEverywhere 中 TextView/EditText 的自定义错误弹出窗口?

android - 如何从android中的警报对话框添加字符串?

android - 在 Sonarqube 仪表板的名称列表中复制 Sonar 项目名称

android - 一般如何动态更改 TextVIew 和 UI?

android - 在有 id 的 relativeLayout 下设置一个没有 id 的 TextView 文本

android - 使用超过 3g 时未找到证书路径的信任 anchor ,但在 WiFi 上工作正常

html - Tailwindcss 遇到问题