Three ways to directly display images in Android TextView

Source: Internet
Author: User

Method 1: override the onDraw method of TextView, which is quite intuitive, that is, it is not good to control the relationship between the positions of the space occupied by the font after the image is displayed. Generally, it is recommended to write this statement if the font is overlapping images. Time relationship, this does not pay the source code.


Method 2: Using TextView to support some Html features, you can directly use the api to assign images. The Code is as follows:

// The first method is to display the image String html = ""; ImageGetter imgGetter = new ImageGetter () {@ Overridepublic Drawable getDrawable (String source) in TextView) {// TODO Auto-generated method stubint id = Integer. parseInt (source); Drawable d = getResources (). getDrawable (id); d. setBounds (0, 0, d. getIntrinsicWidth (), d. getIntrinsicHeight (); return d ;}}; CharSequence charSequence = Html. fromHtml (html, imgGetter, null); textView1.setText (charSequence); textView1.append ("hello ");

Note the following sentence: String html = ""; in addition to ", img src = is followed by a 'sign. If you remove the two signs, it becomes String html = ""; an error is returned because src is directly equal to a number and cannot be parsed.


Method 3: Use ImageSpan and SpannableString. The Code is as follows:

// 2nd Methods: display the image Bitmap B = BitmapFactory in TextView. decodeResource (getResources (), R. drawable. hanguo); ImageSpan imgSpan = new ImageSpan (this, B); SpannableString spanString = new SpannableString ("icon"); spanString. setSpan (imgSpan, 0, 4, Spannable. SPAN_EXCLUSIVE_EXCLUSIVE); textView2.setText (spanString, he claimed that he was responsible for the sinking event of South Korea's "years" and announced his resignation. He hoped that his family could forgive and understand his decision. ");

This method is the most intuitive. Get the ImageSpan object through Bitmap or Drawable object, create a new SpannableString object, and set the span content to OK. In fact, SpannableString is very powerful, such as in EditText will be part of the text highlight, underline, italic, insert expression can use it, see the link: http://gundumw100.iteye.com/blog/904107

Http://blog.csdn.net/rockcoding/article/details/7231756

The following two graphs correspond to the second and third methods respectively:




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.