Android TextView使用HTML代码
来源:互联网 发布:软件项目实施工作总结 编辑:程序博客网 时间:2024/05/17 08:07
我们平常使用TextView的setText()方法传递String参数的时候,其实是调用的public final void setText (CharSequence text)方法:
/**
* Sets the string value of the TextView. TextView <em>does not</em> accept
* HTML-like formatting, which you can do with text strings in XML resource files.
* To style your strings, attach android.text.style.* objects to a
* {@link android.text.SpannableString SpannableString}, or see the
* <a href="{@docRoot}guide/topics/resources/available-resources.html#stringresources">
* Available Resource Types</a> documentation for an example of setting
* formatted text in the XML resource file.
*
* @attr ref android.R.styleable#TextView_text
*/
@android.view.RemotableViewMethod
public final void setText(CharSequence text) {
setText(text, mBufferType);
}
而String类是CharSequence的子类,在CharSequence子类中有一个接口Spanned,即类似html的带标记的文本,我们可以用它来在TextView中显示html。但在上面Android源码注释中有提及TextView does not accept HTML-like formatting。
android.text.Html类共提供了三个方法,可以到Android帮助文档查看。
public static Spanned fromHtml (String source)
public static Spanned fromHtml (String source, Html.ImageGetter imageGetter, Html.TagHandler tagHandler)
public static String toHtml (Spanned text)
通过使用第一个方法,可以将Html显示在TextView中:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView tv=(TextView)findViewById(R.id.textView1);
String html="<html><head><title>TextView使用HTML</title></head><body><p><strong>强调</strong></p><p><em>斜体</em></p>"
+"<p><a href=\"http://www.dreamdu.com/xhtml/\">超链接HTML入门</a>学习HTML!</p><p><font color=\"#aabb00\">颜色1"
+"</p><p><font color=\"#00bbaa\">颜色2</p><h1>标题1</h1><h3>标题2</h3><h6>标题3</h6><p>大于>小于<</p><p>" +
"下面是网络图片</p><img src=\"http://avatar.csdn.net/0/3/8/2_zhang957411207.jpg\"/></body></html>";
tv.setMovementMethod(ScrollingMovementMethod.getInstance());//滚动
tv.setText(Html.fromHtml(html));
}
- Android TextView使用HTML代码
- android中textview使用 html
- android textview 使用html样式
- 使用TextView加载部分HTML代码
- Android TextView 使用HTML格式串
- android textview 使用html实现下划线效果
- Android TextView 显示HTML代码以及代码中的图片标签
- Android 使用代码为textview设置drawableLeft
- Android使用代码为textview设置drawableLeft
- 使用Textview显示包含图片的html代码
- android TextView 显示HTML
- Android TextView HTML链接
- Android TextView show Html
- TextView 使用HTML标签
- textView 使用HTML标签
- android的TextView使用Html(图片显示)格式。。。。。。
- android的TextView使用Html(图片显示)格式。。。。。。
- Android TextView使用HTML处理字体样式、显示图片等
- 未解决问题2_Unknown rpc kind RPC_WRITABLE
- 关于AutoCompleteTextView设置背景无效的解决办法
- Linux awk
- [K/3Cloud] KSQL 关联表更新字段Update语法
- AJAX
- Android TextView使用HTML代码
- 自定义checkbox和radio样式
- iOS7新特征汇总[01]新特征介绍1
- 克里斯的交付能力的时刻没年费离开倒霉死了看
- MTK6572 平台G_SEENSOR兼容
- 消除之王
- IOS正则表达式
- mssql:between and 用法
- win7下vs2008,vs2010编译sqlite源码,生成sqlite3.lib