TextView textView=(TextView)findViewById(R.id.hello);
textView.setText(Html.fromHtml("Hello <b>World</b>,<font size=\"3\" color=\"red\">AnalysisXmlActivty!</font>"));
android 支持的Html标签
<a href="..."> 定义链接内容 <b> 定义粗体文字 b 是blod的缩写 <big> 定义大字体的文字 <blockquote> 引用块标签 属性: Common -- 一般属性 cite -- 被引用内容的URI <br> 定义换行 <cite> 表示引用的URI <dfn> 定义标签 dfn 是defining instance的缩写 <div align="..."> <em> 强调标签 em 是emphasis的缩写 <font size="..." color="..." face="..."> <h1> <h2> <h3> <h4> <h5> <h6> <i> 定义斜体文字 <img src="..."> <p> 段落标签,里面可以加入文字,列表,表格等 <small> 定义小字体的文字 <strike> 定义删除线样式的文字 不符合标准网页设计的理念,不赞成使用. strike是strikethrough的缩写 <strong> 重点强调标签 <sub> 下标标签 sub 是subscript的缩写 <sup> 上标标签 sup 是superscript的缩写 <tt> 定义monospaced字体的文字 不赞成使用. 此标签对中文没意义 tt是teletype or monospaced text style的意思 <u> 定义带有下划线的文字 u是underlined text style的意思
在strings.xml中定义字符串时,可能会需要用到HTML标签,
但是在使用这些标签的时候要注册将“<”改成HTML转义符<,如下:
<resources> <string name="welcome_messages">Hello, %1$s! You have <b>%2$d new messages</b>.</string> </resources>
如果需要加入其它的HTML标签,可将带HTML标答的字符串内容放在<![CDATA[和]]>里面,如下:
<string name="htmlsource"><![CDATA[<p>段落1</p><p>段落2<h1>标题1</h1> 正文1 (<i>斜体</i>) 正文2 (<i>斜体</i>) 正文3 <b>加粗</b> 正文4 \"引号\". 正文5</p><p>段落3</p>]]></string>
在代码中调用字符串时,用
Resources res = getResources();
String text = String.format(res.getString(R.string.welcome_messages), username, mailCount);
CharSequence styledText = Html.fromHtml(text);