`
gybin
  • 浏览: 265055 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Tags Supported by Html.fromHtml()

 
阅读更多

Here are a few tips:

Links in TextViews

The simplest way to add links to TextView is to use the android:autoLink attribute. However, this only allows you to link visible URIs in the text and can sometimes lead to undesired situations (I had an app that was detecting the copyright dates "2009-2011" as a phone number). By using the <a> tag, you can create links with any text that leads to any URI:

<string name="my_site"><a href="http://idunnolol.com">My Website</a></string>


There's only one catch: when you try to use this in a TextView, it won't be clickable unless you set the movement method in code:

TextView myTextView = (TextView) findViewById(R.id.my_textview);
myTextView.setMovementMethod(LinkMovementMethod.getInstance());


Dynamic Html

All of the above examples have been with static text - that is, when the TextView's android:text attribute is set in XML from a string resource. What if you want to set a TextView's string resource in code? This is especially important when you use string formatting (which I'm a big fan of).

Android discusses this already, but for posterity I'll repeat the process here:

1. HTML-escape the string resource:

<string name="loud">Loud text here: &lt;b>%s&lt;/b></string>


2. Use Html.fromHtml() to dynamically create a Spanned that a TextView can be styled with:

Spanned spanned = Html.fromHtml(context.getString(R.string.loud, "this is loud"));
myTextView.setText(spanned);


Handling Custom Tags

So far so good, but I ran into a problem the other day: while the <strike> tag works if it's linked as a static string, it doesn't work when using Html.fromHtml(). I looked through the source code and discovered that Html.fromHtml() handles a different set of tags from static resources. Luckily, there's a way to handle tags that Html.fromHtml() doesn't: the TagHandler interface.

It's a little complex, but here's one possible solution for how to handle the <strike> tag. (My own solution differed a bit, but the essentials are the same.)

Tags Supported in String Resources

Tags in static string resources are parsed by android.content.res.StringBlock, which is a hidden class. I've looked through the class and determined which tags are supported:

  • <a> (supports attributes "href")
  • <annotation>
  • <b>
  • <big>
  • <font> (supports attributes "height", "size", "fgcolor" and "bicolor", as integers)
  • <i>
  • <li>
  • <marquee>
  • <small>
  • <strike>
  • <sub>
  • <sup>
  • <tt>
  • <u>


Tags Supported by Html.fromHtml()

For some reason, Html.fromHtml() handles a different set of of tags than static text supports. Here's a list of the tags (gleaned from Html.java's source code):

  • <a> (supports attribute "href")
  • <b>
  • <big>
  • <blockquote>
  • <br>
  • <cite>
  • <dfn>
  • <div>
  • <em>
  • <font> (supports attributes "color" and "face")
  • <i>
  • <img> (supports attribute "src". Note: you have to include an ImageGetter to handle retrieving a Drawable for this tag)
  • <p>
  • <small>
  • <strong>
  • <sub>
  • <sup>
  • <tt>
  • <u>


The font "color" attribute supports some color names (along with the normal integer-based color scheme):

  • aqua
  • black
  • blue
  • fuchsia
  • green
  • grey
  • lime
  • maroon
  • navy
  • olive
  • purple
  • red
  • silver
  • teal
  • white
  • yellow
分享到:
评论

相关推荐

    JavaEE 5.0 Tutorial.pdf

    Sun 官方 J2ee 5.0 教程 The Java EE 5Tutorial For Sun Java System Application Server 9.1 Contents Preface ..................................................................................................

    Learning the vi and Vim Editors, Seventh Edition

    Moving Around Windows (Getting Your Cursor from Here to There) 180 Moving Windows Around 181 Resizing Windows 183 Buffers and Their Interaction with Windows 186 Playing Tag with Windows 190 Tabbed ...

    Complete Reference-HTML&CSS.5th.edition.pdf

    The book covers all the elements supported in today's Web browsers--from the standard (X)HTML tags to the archaic and proprietary tags that may be encountered. HTML & CSS: The Complete Reference, ...

    EurekaLog_7.5.0.0_Enterprise

    33)..Changed: Default template of HTML/web dialog now includes call stack by default 34)..Changed: EurekaLog 7 now can be installed over EurekaLog 6 automatically, with no additional actions/tools ...

    ZendFramework中文文档

    10.2.3. Fetching Results from a SELECT Statement 10.2.3.1. Fetching a Single Row from a Result Set 10.2.3.2. Fetching a Complete Result Set 10.2.3.3. Changing the Fetch Mode 10.2.3.4. Fetching a ...

    flash标签云 3D效果 PHP插件 by weefselkweekje

    Type the hexadecimal color value you'd like to use for the tags, but not the '#' that usually precedes those in HTML. Black (000000) will obviously work well with light backgrounds, white (ffffff) is ...

    Velocity Suite 1.2.3

    #include( \"disclaimer.txt\", \"opinion.txt\" )] is not currently supported by Third Party Tags or Translators. I suggest adding this extension to your alerts on Macromedia Exchange since I am ...

    php.ini-development

    documents, however this remains supported for backward compatibility reasons. ; Note that this directive does not control the , which can be ; used regardless of this directive. ; Default Value: On ;...

    WPTools.v6.29.1.Pro

    http://www.wpcubed.com/manuals/wp5man/index.html?localization.htm Also see demo\tasks\localization. There are the required XML files. - if flag wpDontAddExternalFontLeading is active in property ...

    LINQPad_Premium_5.36.03_Any_CPU 含破解

    When using LPRun to execute scripts via the command-line, you can now control the %errorlevel% explicitly - either by returning an integer value from Main or by setting Environment.ExitCode....

    Clever Internet Suite (SRC) v9.1.0.0

    HTML Parser - parse HTML Tags: links, images, tables, meta tags and many more. GZip compressor component - provides compressing / uncompressing feature when transmitting data over the Internet, ...

    FastReport.v4.9.81 for.Delphi.BCB.Full.Source企业版含ClientServer中文修正版

    It supports simple HTML tags (b,i,u,strike,sub,sup,font color), all styles of text alignment, text rotation, fill, frame, WYSIWYG mode. screenshot UNICODE support. Your report may display all the ...

    cutEditor -最好用的在线编辑器

    The Paste from Word button solves this by removing word markup before pasting the text into your page. 对于开发人员来说简单易用 The perfect addition to your content management system! Only a ...

    cuteEditor6.0

    In either mode tags can be created by using shift+enter. 支持代码缩进和小写字母 &lt;br/&gt;Cute Editor displays nicely indented code in the HTML mode and the generating HTML tags and ...

    JSP Simple Examples

    In a jsp we should always try to use jsp- style comments unless you want the comments to appear in the HTML. Jsp comments are converted by the jsp engine into java comments in the source code of the ...

    Sublime Text Build 3124 x64 Setup.exe

    minihtml: Fixed a stack overflow on Windows with too many unclosed tags API: Added Phantom and PhantomSet API: Added ViewEventListener API: Added View.is_primary() API: Added EventListener.on_hover...

    DDTCMS 0.2.3.pre.20090808

    a CMS based on django 1.0,supported by Django Dream Team,created by huyoo353 on 2008-NOV-10. -------------------------------------------------------------------------------- ddtcms ver0.1一些说明...

    S7A驱动720版本

    - The number of supported NetLinks has been increased from 4 to 64 - Communication error messages will be now displayed in the alarm history of iFIX. This helps to detect, trace and solve ...

    Cornerstone_2.7.18 破解补丁(Mac)

    • Add existing working copies by dragging from Finder • Change summary (including available repository updates), top folder status and activity shown in source list • Displays local and remote file...

    FastReport.v4.15 for.Delphi.BCB.Full.Source企业版含ClientServer中文修正版支持D4-XE5

    + [enterprise] added property "BrowserPrint" in server configuration - allow printing by browser, added new template nav_print_browser.html + [enterprise] added dynamic file name generation of ...

Global site tag (gtag.js) - Google Analytics