在TextView中使用size HTML属性

时间:2011-08-30 17:13:28

标签: android

我有以下内容:

textView.setText(Html.fromHtml("<font color=\"red\" size=\"24\">Hello</font>"));

字符串'Hello'确实变为红色,但大小不会改变。

好像只是忽略了size属性,有人知道这是为什么吗?我做错了吗?

5 个答案:

答案 0 :(得分:12)

尺寸属性似乎无效。

您可以使用<small><big>(多次增加效果)

您也可以<h1>使用<h6>(仅限标题,即添加新行)

它的旧时尚,但效果很好!

答案 1 :(得分:10)

是的,size属性只是被忽略了。只考虑“颜色”和“面部”属性。

来自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);
}

答案 2 :(得分:2)

试试这个,它为我工作,使用小的,大的关键词

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>"));

答案 3 :(得分:1)

Sergey Gotov是对的。更改文字大小的唯一方法是使用h1 - h6标记。

编辑:您还可以实施TagHandler并使用自己的代码。

答案 4 :(得分:-2)

在Android开发者网站上查看格式和样式
http://developer.android.com/guide/topics/resources/string-resource.html#FormattingAndStyling

或者,在StackOverflow上这篇旧帖子中:
Highlighting Text Color using Html.fromHtml() in Android?