HTML formatting for TextView

痴心易碎 提交于 2019-12-05 10:37:48
user370305

Actually, From the Android Docs..

public final void setText (CharSequence text)

Sets the string value of the TextView. TextView does not 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 SpannableString, or see the Available Resource Types documentation for an example of setting formatted text in the XML resource file.

But,

public final void setText (int resid)
  • no more specification on it..

But from Android Resource String docs..

You can add styling to your strings with HTML markup. For example:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="welcome">Welcome to <b>Android</b>!</string>
</resources>

Supported HTML elements include:

<b> for bold text.
<i> for italic text.
<u> for underline text.

Sometimes you may want to create a styled text resource that is also used as a format string. Normally, this won't work because the String.format(String, Object...) method will strip all the style information from the string. The work-around to this is to write the HTML tags with escaped entities, which are then recovered with fromHtml(String), after the formatting takes place.

And about your URL string,...

tvMyTextView.setText(Html.fromHtml("Click <a href="http://www.poon-world.com">here</a> to switch on the red light.\n"));

Also look at this SO Question Set TextView text from html-formatted string resource in XML

and

Android String Resource

tvMyTextView.setText(Html.fromHtml("There are <i>different ways</i> of coding.\n"));

also try

Below link For linkify so automatically website link assign.

http://developer.android.com/resources/articles/wikinotes-linkify.html

To add a few notes to my own questions and after having received the answers so far, I can only conclude that there doesn't seem to be a reliable way that works everywhere.

In some cases - according to my experience, if a formatted URL is part of the plain text and not enclosed by tags (like http://www.poon-world.com , even just " Poon-World.com " seems to work in most cases), simply setting the properties of the TextView seems to be enough and the links will be clickable. However, if links are embedded in HTML tags and supposed to be clickable from some link text, there seems to be no other way than to go with Html.fromHtml(..).

But there are also a few special cases I can't explain: in some activities/layouts, I am using "embedded" URLs and have set the Click-properties mentioned before, don't use Html.fromHtml .. and surprise!, a click on the indeed created links in the text indeed opens the browser, but only after having added the following line in the code in the OnCreate-Event:

myTextView.setMovementMethod(LinkMovementMethod.getInstance());

(I found this trick in another thread, thanks to the author) No idea why, it seems to be the way the string resources are parsed and evaluated by Android. I just mentioned that on top of all that's already been said so that everyone else looking for solutions and gets confused doesn't start to think he's starting to lose his mind - no, just test the approaches mentioned here on this page and one should usually work out.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!