Use of textview SpannableString and ImageSpan classes in android

Source: Internet
Author: User

Use of textview SpannableString and ImageSpan classes in android

Today I will explain the SpannableString and ImageSpan classes.

Bitmap bitmap = null;
Bitmap = BitmapFactory. decodeResource (getResources (), imageIds [arg2% imageIds. length]);

// The first parameter includes the entire image resource and the second specified id
ImageSpan imageSpan = new ImageSpan (NeighborCommunicationMessageActivity. this, bitmap );

// Add the class used by the image in the editing box to replace the specified text with the image. For example, you can replace the image with each other. You will be asked to select

String str = null;
If (arg2 <10 ){
Str = "f00" + arg2;
} Else if (arg2 <100 ){
Str = "f0" + arg2;
} Else {
Str = "f" + arg2;
}

SpannableString spannableString = new SpannableString (str );
SpannableString. setSpan (imageSpan, 0, 4, Spannable. SPAN_EXCLUSIVE_EXCLUSIVE );

//
Content. append (spannableString );

 

 

 

TextView and SpannableString use Daquan there is a text replaced with ImageSpan specifically: SpannableString ss = new SpannableString ("ccccccccccccccc step on ");
Drawable d = getResources (). getDrawable (R. drawable. icon );
D. setBounds (0, 0, d. getIntrinsicWidth (), d. getIntrinsicHeight ());
// Create ImageSpan
ImageSpan span = new ImageSpan (d, ImageSpan. ALIGN_BASELINE );
// Replace text with ImageSpan
Ss. setSpan (span, 18, 19, Spannable. SPAN_INCLUSIVE_EXCLUSIVE );
TxtInfo. setText (ss );

 

At this time, content. append (spannableString) is actually passing the image expression into edittext.

 

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.