不要通过html中font标签的size属性来更改文字的精确大小

  1.         TextView tv=(TextView)findViewById(R.id.textView1);  
  2.         String html="TextView使用HTML

    强调

    斜体

    "
      
  3.                 +"

    超链接HTML入门学习HTML!

    颜色1"  

  4.                 +"

    颜色2

    标题1

    标题2

    标题3

    大于>小于<

    " +  

  5.                 "下面是网络图片

    "
    ;  
  6.           
  7.         tv.setMovementMethod(ScrollingMovementMethod.getInstance());//滚动  
  8.         tv.setText(Html.fromHtml(html));
看到可以用html的font标签改变文字的颜色,我尝试改变文字大小,后来发现不起效果。通过查看源代码发现,
private void handleStartTag(String tag, Attributes attributes) {

        } 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);
    }
    private static void endFont(SpannableStringBuilder text) {
        int len = text.length();
        Object obj = getLast(text, Font.class);
        int where = text.getSpanStart(obj);


        text.removeSpan(obj);


        if (where != len) {
            Font f = (Font) obj;


            if (!TextUtils.isEmpty(f.mColor)) {
                if (f.mColor.startsWith("@")) {
                    Resources res = Resources.getSystem();
                    String name = f.mColor.substring(1);
                    int colorRes = res.getIdentifier(name, "color", "android");
                    if (colorRes != 0) {
                        ColorStateList colors = res.getColorStateList(colorRes);
                        text.setSpan(new TextAppearanceSpan(null, 0, 0, colors, null),
                                where, len,
                                Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                    }
                } else {
                    int c = getHtmlColor(f.mColor);
                    if (c != -1) {
                        text.setSpan(new ForegroundColorSpan(c | 0xFF000000),
                                where, len,
                                Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                    }
                }
            }


            if (f.mFace != null) {
                text.setSpan(new TypefaceSpan(f.mFace), where, len,
                             Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            }
        }
    }
font标签只实现了两个属性就是:color和face。没有实现size属性。所以不能精确设置文字大小。只能通过big,small这样的相对大小设置。

你可能感兴趣的:(Android,View)