andorid,动态设置TextView文本中字体的颜色和格式,用Html.fromHtml方法
2014-08-20 18:28
721 查看
在android中,有一个Html.fromhtml(String source)方法,可以将比如文本
框中的字符串进行HTML格式化,支持的还是很多的。例如:
需要注意的是html的换行符跟java的区别,需要提取出来单独处理,不然无法换行
Android中显示html文件要用Html.fromHtml(...)处理过的返回值,返回值可以成为setText()的参数。
只显示带文本的html可以用下面的方法处理html文件。
显示带图片的html要用下面的方法处理html文件。
ImageGetter 为处理html中<img>的处理器,生成Drawable对象并返回。
创建ImageGetter 主要实现下面的方法,source为<img>标签中src属性的值。
框中的字符串进行HTML格式化,支持的还是很多的。例如:
/** * 字体颜色突出显示 * * @param before * 前缀字符串 * @param color * 要突出字符串颜色值 * @param text * 要突出的字符串 * @param after * 后缀字符串 * @return 处理后字符串 */ public static Spanned fromHtml(String before, String color, String text, String after) { String[] strs = null; if (after.contains("\n")) { strs = null; strs = after.split("\n"); } StringBuffer strB = new StringBuffer(); if (strs != null && strs.length > 0) { for (int i = 0; i < strs.length; i++) { strB.append(strs[i]); if (i != strs.length - 1) { strB.append("<br />"); } } } else { strB.append(after); } return Html.fromHtml(before + "<font color='" + color + "'>" + text + "</font>" + strB.toString()); }
需要注意的是html的换行符跟java的区别,需要提取出来单独处理,不然无法换行
Android中显示html文件要用Html.fromHtml(...)处理过的返回值,返回值可以成为setText()的参数。
只显示带文本的html可以用下面的方法处理html文件。
public static Spanned fromHtml (String source)
显示带图片的html要用下面的方法处理html文件。
public static Spanned fromHtml (String source, Html.ImageGetter imageGetter, Html.TagHandler tagHandler)
ImageGetter 为处理html中<img>的处理器,生成Drawable对象并返回。
创建ImageGetter 主要实现下面的方法,source为<img>标签中src属性的值。
public class AndroidTest2Activity extends Activity { /** Called when the activity is first created. */ TextView tv; EditText et; Button addPic; String ct; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); et=(EditText) this.findViewById(R.id.editText1); tv=(TextView) this.findViewById(R.id.tv); ct="aaa<font color=\"red\">aaa</font>"; addPic=(Button) this.findViewById(R.id.AddPic); addPic.setOnClickListener(new OnClickListener(){ @Override public void onClick(View arg0) { // TODO Auto-generated method stub ct+="<img src=\""+R.drawable.icon+"\"/>"; refreshView(); } }); refreshView(); } private void refreshView(){ et.setText(Html.fromHtml(ct,imageGetter,null)); tv.setText(Html.fromHtml(ct,imageGetter,null)); } ImageGetter imageGetter = new ImageGetter() { @Override public Drawable getDrawable(String source) { int id = Integer.parseInt(source); Drawable d = getResources().getDrawable(id); d.setBounds(0, 0, d.getIntrinsicWidth(), d .getIntrinsicHeight()); return d; } }; }
相关文章推荐
- Android TextView字体颜色设置方法小结
- android TextView的字体颜色设置的多种方法(续)
- textview设置不同格式字体颜色
- Android编程实现TextView字体颜色设置的方法小结
- TextView的字体颜色设置的多种方法
- android:TextView中的文本链接之--设置超链接、颜色、字体等属性
- android 的TextView空间动态设置字体颜色
- android TextView的字体颜色设置的多种方法
- Android设置TextView字体呈现多种颜色的方法
- android:TextView中的文本链接之--设置超链接、颜色、字体等属性
- [Android UI界面] 如何在activity中动态的设置TextView上字体的颜色
- android:TextView中的文本链接之--设置超链接、颜色、字体等属性
- android TextView的字体颜色设置的多种方法
- TextView(怎么用代码动态设置字体颜色)
- android TextView的字体颜色设置的多种方法(转)
- 设置TextView个别文字字体颜色格式
- TextView的字体颜色设置的多种方法
- html代码设置textview字体颜色
- android TextView的字体颜色设置的多种方法
- 设置textview字体颜色的几种方法