Android (Java) Learning Note 139: Inserting text or pictures with HTML in a TextView component

Source: Internet
Author: User

First we look at the code:

1.activity_main.xml:

<LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Xmlns:tools= "Http://schemas.android.com/tools"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"android:orientation= "vertical"Tools:context= "com.himi.textview_img." Mainactivity " >    <TextViewAndroid:id= "@+id/textview"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "@string/hello_world" />     <ImageViewAndroid:layout_width= "Fill_parent"Android:layout_height= "Wrap_content"Android:background= "@drawable/list_item_divide_operate" />    <TextViewAndroid:id= "@+id/textview01"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "@string/hello_world" />     <ImageViewAndroid:layout_width= "Fill_parent"Android:layout_height= "Wrap_content"Android:background= "@drawable/list_item_divide_operate" />    <TextViewAndroid:id= "@+id/textview02"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "@string/hello_world" /></LinearLayout>

2. Then the Mainactivity.java:

 Packagecom.himi.textview_img;Importandroid.app.Activity;Importandroid.graphics.drawable.Drawable;ImportAndroid.os.Bundle;Importandroid.text.Html;ImportAndroid.view.Menu;ImportAndroid.view.MenuItem;ImportAndroid.widget.TextView; Public classMainactivityextendsActivity {PrivateTextView TV; PrivateTextView TV1; PrivateTextView TV2; Private FinalString SText1 = "Test picture information: "; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);         Setcontentview (R.layout.activity_main); TV=(TextView) Findviewbyid (R.id.textview); TV1=(TextView) Findviewbyid (R.ID.TEXTVIEW01); TV2=(TextView) Findviewbyid (R.ID.TEXTVIEW02); //Display HTML text content directly with TextViewTv.settext (html.fromhtml ("     )); //Take advantage of Textview.settext (html.fromhtml (...,......,......)); METHOD displays an IMG image in an HTML file, a resource file pictureTv1.settext (html.fromhtml (STEXT1,NewHtml.imagegetter () { Publicdrawable getdrawable (String source) {drawable drawable=NULL; intRID =integer.parseint (source); Drawable=getresources (). getdrawable (RID); Drawable.setbounds (0,0, Drawable.getintrinsicwidth (), Drawable.getintrinsicheight ()); returndrawable; }                },NULL)); Tv2.settext (Text); } @Override Public BooleanOncreateoptionsmenu (Menu menu) {//inflate the menu; This adds items to the action bar if it is present.getmenuinflater (). Inflate (R.menu.main, menu); return true; } @Override Public Booleanonoptionsitemselected (MenuItem item) {//Handle Action Bar item clicks here. The Action Bar would//automatically handle clicks on the Home/up button, so long//As you specify a the parent activity in Androidmanifest.xml.        intID =Item.getitemid (); if(id = =r.id.action_settings) {            return true; }        return Super. onoptionsitemselected (item); }}

Here's the point: the HTML class and its static method fromhtml ()

Fromhtml () is overloaded and has two formats as follows:

(1) public static spanned fromhtml(String source)

By returning the spanned (inherited from Charsequence): Represents the style text that can be displayed

(2) public static spanned fromhtml(String source,html.imagegetter imagegetter, Html.taghandler taghandler)

Source: a string containing HTML.

imagegetter : Imagegetter object. The Getdrawable method of the Imagegetter object is called when the system resolves to the tag, and the SRC attribute value is passed into the Getdrawable method. As for the specific meaning of the SRC attribute, it is determined in the Getdrawable method. The Getdrawable method returns a Drawable object. We can get image resources from res/drawable resources, assets resources, SD cards and the network, and divide into drawable objects.

Taghandler: The Taghandler object. The Handletag method of the object is called every time the system processes a label. However, this parameter is not commonly used. If not, it can be set to null.

The above mentioned is the project resource file using HTML This class in TextView display, of course, similar or network pictures , mobile phone local image in the use of HTML this class in the TextView display code examples are as follows:

(1) Network Image:

FinalString stext = "Test picture information: <br>"; Tview.settext (html.fromhtml (stext, Imagegetter,NULL));FinalHtml.imagegetter Imagegetter =NewHtml.imagegetter () { Publicdrawable getdrawable (String source) {drawable drawable=NULL;    URL url; Try{URL=NewURL (source); Drawable= Drawable.createfromstream (Url.openstream (), ""); } Catch(Exception e) {e.printstacktrace (); return NULL; } drawable.setbounds (0, 0, Drawable.getintrinsicwidth (), Drawable.getintrinsicheight ()); returndrawable; };}

(2) mobile phone local Image:

FinalString sText2 = "Test picture information: "; Tview.settext (html.fromhtml (sText2, Imagegetter,NULL));FinalHtml.imagegetter Imagegetter =NewHtml.imagegetter () { Publicdrawable getdrawable (String source) {drawable drawable=NULL; Drawable=Drawable.createfrompath (source); Drawable.setbounds (0, 0, Drawable.getintrinsicwidth (), Drawable.getintrinsicheight ()); returndrawable; };}

Pay special attention to the paths of three different images:

(1) resource Files :

(2) Local file:

(3) network files:

Android (Java) Learning Note 139: Inserting text or pictures with HTML in a TextView component

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.