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: