问题描述
我正在尝试给一些文本上色,然后使用Html.fromHtml()
设置控件的text属性,但是它只是返回白色,我不确定为什么。
buyPrice.setText("BUY: " + Html.fromHtml(getColoredSpanned(recommendeditem.getItemLowPrice(), "#ff3c3c")));
private String getColoredSpanned(String text, String color) {
String input = "<font color=" + color + ">" + text + "</font>";
return input;
}
getItemLowPrice
public String getItemLowPrice() {
return String.format(NumberFormat.getInstance(Locale.US).format(Long.parseLong(mLowPrice)));
}
当我单击一个按钮时就会发生这种情况,除了单击按钮之外,
1楼
尝试使用此方法,因为不推荐使用html.fromhtml方法
@SuppressWarnings("deprecation")
public static Spanned fromHtml(String html){
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
return Html.fromHtml(html, Html.FROM_HTML_MODE_LEGACY);
} else {
return Html.fromHtml(html);
}
}
public static final int FROM_HTML_MODE_COMPACT = 63;
public static final int FROM_HTML_MODE_LEGACY = 0;
public static final int FROM_HTML_OPTION_USE_CSS_COLORS = 256;
public static final int FROM_HTML_SEPARATOR_LINE_BREAK_BLOCKQUOTE = 32;
public static final int FROM_HTML_SEPARATOR_LINE_BREAK_DIV = 16;
public static final int FROM_HTML_SEPARATOR_LINE_BREAK_HEADING = 2;
public static final int FROM_HTML_SEPARATOR_LINE_BREAK_LIST = 8;
public static final int FROM_HTML_SEPARATOR_LINE_BREAK_LIST_ITEM = 4;
public static final int FROM_HTML_SEPARATOR_LINE_BREAK_PARAGRAPH = 1;
public static final int TO_HTML_PARAGRAPH_LINES_CONSECUTIVE = 0;
public static final int TO_HTML_PARAGRAPH_LINES_INDIVIDUAL = 1;
尝试阅读有关HTML类文档的不同标志