Android: How to achieve picture-and-text blending in TextView

Source: Internet
Author: User

We usually set the text in the TextView text. But how to set up a picture and text mix it?

I'm going to write an example here. We need to use a bit of simple HTML knowledge

Some HTML-like tags have been booked in TextView, which allows the TextView control to display different colors, sizes, and font text

<font>: setting colors and fonts

<big>: Set Large

<small>: Setting the trumpet

<i>\<b>: Italic, Bold

<a>: Link Address

: Inserting pictures

Deposit our images in the drawable.

Then we add a TextView control to the layout file

<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android"    android:layout_width= "Fill_ Parent "    android:layout_height=" fill_parent "    android:orientation=" vertical ">    <textview        Android:id= "@+id/tv_text"        android:layout_width= "wrap_content"        android:layout_height= "wrap_content"/ ></LinearLayout>
In the Mainactivity.clss file

Write a line of HTML code and receive it with a string

String html= "<font> Strawberry </font>
What does this line of code mean? Everyone also looked at the HTML simple tag above. <font> is the font, so between <font> and </font> is the text, is the picture, SRC is the name of the picture

And then we're going to explain this line of HTML code, and that's the way it's used.

Html.fromhtml (String source, Imagegetter Imagegetter, Taghandler Taghandler)
Where source is a string form of HTML code that we need to interpret. Imagegetter is the resource of the picture, Taghandler the label is set here to null on the line.

So how do you write Imagegetter? We can new a Imagegetter object will find a way inside

Public drawable getdrawable (String source) {}
We can use the output log information to see what source is inside this method

Did you find it? is the content of our HTML code

Since we are returning a resource of type drawable, we first new a Drawable object, and then we get the picture resource through the method of GetSource ().

Finally, be sure to set the size of a piece through the SetBounds () method. Otherwise, it won't show.

if (Source.equals ("' Strawberry ')") {drawable draw=getresources (). getdrawable (R.drawable.strawberry); Draw.setbounds (0, 0, draw.getintrinsicwidth (), Draw.getintrinsicheight ()); return draw;}

Attached picture of running result

The full code for Mainactivity.class is as follows:

Package Com.example.textview;import Javax.xml.transform.source;import Android.os.bundle;import Android.app.activity;import Android.graphics.drawable.drawable;import Android.text.html;import Android.text.html.imagegetter;import Android.util.log;import Android.view.menu;import Android.widget.TextView; public class Mainactivity extends Activity {private TextView tv_text; @Overrideprotected void OnCreate (Bundle Savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); tv_text= ( TextView) Findviewbyid (R.id.tv_text); String html= "<font> Strawberry </font>


Android: How to achieve picture-and-text blending in TextView

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.