我正在开发一个 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/