最新文章专题视频专题问答1问答10问答100问答1000问答2000关键字专题1关键字专题50关键字专题500关键字专题1500TAG最新视频文章推荐1 推荐3 推荐5 推荐7 推荐9 推荐11 推荐13 推荐15 推荐17 推荐19 推荐21 推荐23 推荐25 推荐27 推荐29 推荐31 推荐33 推荐35 推荐37视频文章20视频文章30视频文章40视频文章50视频文章60 视频文章70视频文章80视频文章90视频文章100视频文章120视频文章140 视频2关键字专题关键字专题tag2tag3文章专题文章专题2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章专题3
当前位置: 首页 - 科技 - 知识百科 - 正文

TextView使用HTML标签_html/css

来源:动视网 责编:小采 时间:2020-11-27 16:33:45
文档

TextView使用HTML标签_html/css

TextView使用HTML标签_html/css_WEB-ITnose:关于TextView 的富文本实现方式,有2中,一种使用Html来做,一种是使用SpannableString; SpannableString太麻烦了,这里有参考: http://www.chawenti.com/articles/16016.html 关于 TextView使用Html,可以参考 这篇blog http:
推荐度:
导读TextView使用HTML标签_html/css_WEB-ITnose:关于TextView 的富文本实现方式,有2中,一种使用Html来做,一种是使用SpannableString; SpannableString太麻烦了,这里有参考: http://www.chawenti.com/articles/16016.html 关于 TextView使用Html,可以参考 这篇blog http:
 关于TextView 的富文本实现方式,有2中,一种使用Html来做,一种是使用SpannableString;

SpannableString太麻烦了,这里有参考:

http://www.chawenti.com/articles/16016.html


关于 TextView使用Html,可以参考 这篇blog

http://blog.csdn.net/johnsonblog/article/details/7741972#comments

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)


经常使用的是第一个方法:

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

强调

斜体

" +"

超链接HTML入门学习HTML!

颜色1" +"

颜色2

标题1

标题2

标题3

大于>小于<

" + "下面是网络图片

"; tv.setMovementMethod(ScrollingMovementMethod.getInstance());//滚动 tv.setText(Html.fromHtml(html));


要实现图片的显示需要使用Html.fromHtml的另外第二个重构方法:

public static Spanned fromHtml (String source, Html.ImageGetterimageGetter, Html.TagHandler tagHandler)

其中Html.ImageGetter是一个接口,我们要实现此接口,在它的getDrawable(String source)方法中返回图片的Drawable对象才可以。

ImageGetter imgGetter = new Html.ImageGetter() { public Drawable getDrawable(String source) { Drawable drawable = null; URL url; try { url = new URL(source); drawable = Drawable.createFromStream(url.openStream(), ""); //获取网路图片 } catch (Exception e) { return null; } drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable .getIntrinsicHeight()); return drawable; }};


这里主要是实现了Html.ImageGetter接口,通过图片的URL地址获取相应的Drawable实例。

上面介绍的是显示网络上的图片,但如何显示本地的图片呢:

 ImageGetter imgGetter = new Html.ImageGetter() { public Drawable getDrawable(String source) { Drawable drawable = null; drawable = Drawable.createFromPath(source); //显示本地图片 drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable .getIntrinsicHeight()); return drawable; }};

只需将source改为本地图片的路径便可,在这里我使用的是:

String source;
source=getFilesDir()+"/ic_launcher.png";

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

这里主要讲一种场合的使用,来源

http://www.gxlcms.com/article/363.htm

然而,有一种场合,默认支持的标签可能不够用。比如,我们需要在textView中点击某种链接,返回到应用中的某个界面,而不仅仅是网络连接,如何实现?

Html类中有一个接口类处理点击标签的处理的:

/** * Is notified when HTML tags are encountered that the parser does * not know how to interpret. */ public static interface TagHandler { /** * This method will be called whenn the HTML parser encounters * a tag that it does not know how to interpret. */ public void handleTag(boolean opening, String tag, Editable output, XMLReader xmlReader);


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 
for each
// so we can safely emite the linebreaks when we handle the close tag. } else if (tag.equalsIgnoreCase("p")) { handleP(mSpannableStringBuilder); } else if (tag.equalsIgnoreCase("div")) { handleP(mSpannableStringBuilder); } else if (tag.equalsIgnoreCase("em")) { start(mSpannableStringBuilder, new Bold()); } else if (tag.equalsIgnoreCase("b")) { start(mSpannableStringBuilder, new Bold()); } .................. } else if (tag.length() == 2 && Character.toLowerCase(tag.charAt(0)) == 'h' && tag.charAt(1) >= '1' && tag.charAt(1) <= '6') { handleP(mSpannableStringBuilder); start(mSpannableStringBuilder, new Header(tag.charAt(1) - '1')); } else if (tag.equalsIgnoreCase("img")) { startImg(mSpannableStringBuilder, attributes, mImageGetter); } else if (mTagHandler != null) { mTagHandler.handleTag(true, tag, mSpannableStringBuilder, mReader); } } private void handleEndTag(String tag) { if (tag.equalsIgnoreCase("br")) { handleBr(mSpannableStringBuilder); } else if (tag.equalsIgnoreCase("p")) { handleP(mSpannableStringBuilder); } else if (tag.equalsIgnoreCase("div")) { handleP(mSpannableStringBuilder); } else if (tag.equalsIgnoreCase("em")) { end(mSpannableStringBuilder, Bold.class, new StyleSpan(Typeface.BOLD)); } else if (tag.equalsIgnoreCase("b")) { end(mSpannableStringBuilder, Bold.class, new StyleSpan(Typeface.BOLD)); } ........................ ........................ } else if (tag.length() == 2 && Character.toLowerCase(tag.charAt(0)) == 'h' && tag.charAt(1) >= '1' && tag.charAt(1) <= '6') { handleP(mSpannableStringBuilder); endHeader(mSpannableStringBuilder); } else if (mTagHandler != null) { mTagHandler.handleTag(false, tag, mSpannableStringBuilder, mReader); } }

如果不是默认的标签,会调用mTagHandler的handleTag方法。所以,我们可以实现此接口,来解析自己定义的标签类型。


自定义一个标签,实现接口

public class GameTagHandler implements TagHandler { private int startIndex = 0; private int stopIndex = 0; @Override public void handleTag(boolean opening, String tag, Editable output, XMLReader xmlReader) { if (tag.toLowerCase().equals("game")) { if (opening) { startGame(tag, output, xmlReader); } else { endGame(tag, output, xmlReader); } } } public void startGame(String tag, Editable output, XMLReader xmlReader) { startIndex = output.length(); } public void endGame(String tag, Editable output, XMLReader xmlReader) { stopIndex = output.length(); output.setSpan(new GameSpan(), startIndex, stopIndex, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); } private class GameSpan extends ClickableSpan implements OnClickListener { @Override public void onClick(View v) { // 跳转某页面 ,自己实现跳转的动作,就可以点击TextView中的指定文字进行自定义的动作的了 } }

然后在工程中调用方法:

textView.setText(Html.fromHtml(“点击这里跳转到游戏”,null, new GameTagHandler()));

textView.setClickable(true);

textView.setMovementMethod(LinkMovementMethod.getInstance());

文档

TextView使用HTML标签_html/css

TextView使用HTML标签_html/css_WEB-ITnose:关于TextView 的富文本实现方式,有2中,一种使用Html来做,一种是使用SpannableString; SpannableString太麻烦了,这里有参考: http://www.chawenti.com/articles/16016.html 关于 TextView使用Html,可以参考 这篇blog http:
推荐度:
标签: 标签 html css
  • 热门焦点

最新推荐

猜你喜欢

热门推荐

专题
Top