Three ways to display images directly in Android TextView

Source: Internet
Author: User

Method One: Rewrite TextView's OnDraw method, but also very intuitive is not very good control after displaying the picture and then display the position of the space occupied by the font. It is generally assumed that the font is overlapping on the picture recommended for this write. Time relationship, this does not pay the source code.


Method Two: Using TextView to support the features of some HTML. Use the API to assign pictures directly. The code is as follows:

The first method displays the picture in TextView, string html = "

Note the following sentence: String html = "


Method Three: Use Imagespan and spannablestring. The code is as follows:

The 2nd method displays the picture in TextView bitmap B = Bitmapfactory.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); Textview2.append (Xinhua Beijing, April 27, according to CCTV reports, South Korea's prime Minister Zheng Bake in the local time 27th morning, announced the meeting, said that his response to the Korean "years "The wreck was responsible, announcing his resignation and hoping that the family would forgive and understand his decision." ");

Such a method is the most intuitive. Gets the Imagespan object by bitmap or Drawable object. To create a new Spannablestring object, set the contents of the span to OK. In fact, spannablestring is very powerful, such as in the EditText will be part of the text highlighting, underline, italic, insert expression can use it, see Links: http://gundumw100.iteye.com/blog/904107

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

Next, the upper and lower pictures correspond to another and the third method:




Three ways to display images directly in Android TextView

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.