【发布时间】:2011-11-07 00:11:24
【问题描述】:
我有以下几点:
textView.setText(Html.fromHtml("<font color=\"red\" size=\"24\">Hello</font>"));
字符串 'Hello' 确实变红了,但大小没有改变。
好像 size 属性被忽略了,有谁知道这是为什么?我做错了吗?
【问题讨论】:
标签: android
我有以下几点:
textView.setText(Html.fromHtml("<font color=\"red\" size=\"24\">Hello</font>"));
字符串 'Hello' 确实变红了,但大小没有改变。
好像 size 属性被忽略了,有谁知道这是为什么?我做错了吗?
【问题讨论】:
标签: android
是的,size 属性只是被忽略了。仅考虑“颜色”和“面部”属性。
private void handleStartTag(String tag, Attributes attributes) {
if (tag.equalsIgnoreCase("br")) {
// We don't need to handle this. TagSoup will ensure that there's a </br> for each <br>
// so we can safely emite the linebreaks when we handle the close tag.
}
...
else if (tag.equalsIgnoreCase("font")) {
startFont(mSpannableStringBuilder, attributes);
}
...
}
private static void startFont(SpannableStringBuilder text,
Attributes attributes) {
String color = attributes.getValue("", "color");
String face = attributes.getValue("", "face");
int len = text.length();
text.setSpan(new Font(color, face), len, len, Spannable.SPAN_MARK_MARK);
}
【讨论】:
AbsoluteSizeSpan类。
谢尔盖·戈托夫是对的。更改文本大小的唯一方法是使用h1 - h6 标签。
编辑:你也可以实现TagHandler并使用你自己的标签。
【讨论】:
查看 Android 开发者网站上的Formatting and Styling:
http://developer.android.com/guide/topics/resources/string-resource.html#FormattingAndStyling
或者,在 StackOverflow 的这篇旧帖子上:
Highlighting Text Color using Html.fromHtml() in Android?
【讨论】:
Size 属性似乎不起作用。
你可以使用<small>或<big>(多次增加效果)
您也可以使用<h1> 到<h6>(仅限标题,即添加新行)
它是老式的,但效果很好!
【讨论】:
<small><sup><sup><small><small><small>2</small></small></small></small></sup></sup>
试试这个,它对我有用,使用大小关键字
TextView mBox = (TextView) findViewById(R.id.txt);
mBox.setText(Html.fromHtml("<font color=#cc0029>" + "<b>"
+ "Hiiiiiiiiii" + "</b>" + "<br />" + "<small>" + "description"
+ "</small>" + "<br />" + "<small>" + "DateAdded" + "</small>"));
【讨论】: