Android: How to Implement text-and-image mixing in TextView

Source: Internet
Author: User

Android: How to Implement text-and-image mixing in TextView

We usually set text in TextView text. But how do I set a text-and-text hybrid arrangement?

I will write an example here. We need a little simple HTML knowledge.

Some HTML-like labels are reserved in TextView. The TextView control can be used to display text in different colors, sizes, and fonts.

: Set color and font

: Set Large Size

: Set the trumpet

: Italic and bold

: Link address

: Insert an image

Store our images in drawable.

Then we add a TextView control to the layout file.

<a><linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical"><font><big><small>    <textview android:id="@+id/tv_text" android:layout_width="wrap_content" android:layout_height="wrap_content"></textview></small></big></font></linearlayout></a>
In the MainActivity. clss File

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

String html ="Strawberry ";
What does this line of code mean? We also read the simple HTML Tag above. It is a font, so it is a text, an image, and src is the image name.

Then we need to explain this line of HTML code and use this method.

Html.fromHtml(String source, ImageGetter imageGetter, TagHandler tagHandler)
Here, source is the HTML code in the string format that we need to explain. ImageGetter is the image resource, and the TagHandler label is set to null here.

How can I write ImageGetter? We can create an ImageGetter object and find a method in it.

public Drawable getDrawable(String source) {}
We can use the output log information to see what the source is passed in this method.

Found? Is the content in our Html code.

Because the returned resource is a Drawable type, we first create a Drawable object, and then obtain the image resource through the getSource () method.

At last, you must use the setBounds () method to set the size of a piece. Otherwise it will not be displayed.

 

if(source.equals("http://blog.csdn.net/su20145104009/article/details/‘strawberry’")){Drawable draw=getResources().getDrawable(R.drawable.http://blog.csdn.net/su20145104009/article/details/strawberry);draw.setBounds(0, 0, draw.getIntrinsicWidth(), draw.getIntrinsicHeight());return draw;}

Attached running result image

 

The 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 = "strawberry"; CharSequence text = Html. fromHtml (html, new ImageGetter () {public Drawable getDrawable (String source) {// obtain the Image Log Based on the image resource ID. d ("source", source); if (source. equals ("http://blog.csdn.net/su20145104009/article/details/'strawberry'") {Drawable draw = getResources (). getDrawable (R. drawable. http://blog.csdn.net/su20145104009/article/details/strawberry); draw. setBounds (0, 0, draw. getIntrinsicWidth (), draw. getIntrinsicHeight (); return draw;} return null ;}}, null); TV _text.setText (text );}}

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.