When I was learning Android, I suddenly came up with a question: how can I use the textview control to display formatted text? Can I use HTML layout? I checked the android help documentation and provided the Android. Text. html class and the HTML. imagegetter and HTML. taghandler interfaces.
Actually, I didn't plan to write this blog post, but I saw articles about this on the internet, basically: You copied me, I copied you, And you copied them, one or two articles are useful, and the network is like this. piracy has become a kind of culture. This is what we call "tailism. Of course, I do not deny the hard work of Daniel and write high-quality articles. Secondly, I learned what I learned and used my personal habits.
First look:
When we use the settext () method of textview to pass the string parameter, it is actually the public
Final void settext (charsequence text) method:
/** * 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); }
The string class is a subclass of charsequence. In the charsequence subclass, there is an interface spanned, which is similar to HTML text with tags. We can use it to display HTML in textview. However, the above Android source code comment mentions textview does not accept HTML-like.
Formatting.
The android. Text. html class provides three methods, which can be viewed in the android help document.
public static Spanned fromHtml (String source)public static Spanned fromHtml (String source, Html.ImageGetter imageGetter, Html.TagHandler tagHandler)public static String toHtml (Spanned text)
By using the first method, you can display HTML in textview:
Public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); textview TV = (textview) findviewbyid (R. id. textview1 ); string html = "<HTML>
Effect:
It can be seen that the font effect is displayed, but the image is not displayed. HTML is required for image display. fromhtml another reconstruction method: public static spanned fromhtml (string source, HTML. imagegetterimagegetter, HTML. taghandler) Where HTML. imagegetter is an interface. To implement this interface, you can return the drawable object of the image in its getdrawable (string source) method.
Modified code:
Imagegetter imggetter = new HTML. imagegetter () {public drawable getdrawable (string source) {drawable = NULL; Url URL; try {url = new URL (source); drawable = drawable. createfromstream (URL. openstream (), ""); // obtain the network image} catch (exception e) {return NULL;} drawable. setbounds (0, 0, drawable. getintrinsicwidth (), drawable. getintrinsicheight (); Return drawable ;}};
Here we mainly implement the HTML. imagegetter interface to obtain the corresponding drawable instance through the image URL.
Do not forget to add the network access permission to the mainifest file:
<uses-permission android:name="android.permission.INTERNET" />
Tip: it is a time-consuming operation to obtain images through the network. It is best not to place the images in the main thread; otherwise, it may cause blocking.
The above describes how to display images on the network, but how to display local images:
Imagegetter imggetter = new HTML. imagegetter () {public drawable getdrawable (string source) {drawable = NULL; drawable = drawable. createfrompath (source); // display the local image drawable. setbounds (0, 0, drawable. getintrinsicwidth (), drawable. getintrinsicheight (); Return drawable ;}};
You only need to change source to the path of the local image. Here I use:
String source; source=getFilesDir()+"/ic_launcher.png";
The end