Using size HTML attribute in TextView
I have the following:
textView.setText(Html.fromHtml("<font color=\"red\" size=\"24\">Hello</font>"));
The string 'Hello' does turn red but the size does not change.
It is as if the size attribute is just ignored, does anyone know why this is? Am I doing something w开发者_开发技巧rong?
Size attribute seems not working.
You can use <small>
or <big>
(multiple times to increase the effect)
You can also use <h1>
to <h6>
(Header only, i.e. add a new line)
Its old-fashion, but it works well !
Yes, size attribute just ignored. Only "color" and "face" attributes are taken into account.
From Html
class sources:
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);
}
Try this one,Its working for me,use small,big key words
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>"));
Sergey Gotov is right. The only way to change text size it to use h1
- h6
tags.
EDIT: You can also implement TagHandler
and use your own tags.
Look at Formatting and Styling on the android developpers site:
http://developer.android.com/guide/topics/resources/string-resource.html#FormattingAndStyling
Or, on this old post of StackOverflow :
Highlighting Text Color using Html.fromHtml() in Android?
精彩评论