SpannableString for Android development and android Development

Source: Internet
Author: User

SpannableString for Android development and android Development

In the actual application development process, it is often encountered that different text parts display different font style information, such as text font, size, color, style, and hyperlink. In general, the text in TextView is a style. in similar cases, you can use the SpannableString or SpannableStringBuilder object to implement the above settings.

Both SpannableString and SpannableStringBuilder can set a text segment to a Span. In Android, Span represents the effect of a text segment, such as the link format, image, and background text. The difference is that SpannableStringBuilder can modify the text content.

Click it to get familiar with the SpannableString class.

Class Structure:

Extends Object
Implements GetChars Spannable CharSequence

Java. lang. Object


Android. text. SpannableString

Class Overview:

The content of such text remains unchanged, but the marking object can be appended and separated. For variable text, seeSpannableStringBuilder.


Public Constructors


SpannableString (CharSequence source)


Common public methods:

Public Methods

Final char

CharAt (int I)

Returns the character at the specified index position. The index of the first character is 0.


Equals (Object o)

Compares this instance with the specified object and indicates if they are equal.

Compare this instance with the specified object. If it is equal, true is returned.

Final void

GetChars (int start, int end, char [] dest, int off)

Similar to the String. getChars () method: copy characters from the specified character array. Start position, end position, and off offset.


GetSpanEnd (Object what)

Obtain the end position of what object in the SpannableString object. If no match exists,-1 is returned.


GetSpanStart (Object what)

Obtain the starting position of what object in the SpannableString object. If no match exists,-1 is returned.

Final int

Length ()

Returns the number of characters in the sequence.


RemoveSpan (Object what)

Deletes a specified object from the text range.


SetSpan (Object what, int start, int end, int flags)

Sets a Span object.



Void setSpan (Object what, int start, int end, int flags)

Sets a Span object. The first parameter "what" indicates the effect of a piece of text. It can accept the following attributes:

Attribute name



Background Color


Clickable text with click events


Text color (foreground color)


Blur (BlurMaskFilter) embossed (EmbossMaskFilter)


Parent class, generally not used


Grating effect


Strikethrough (hyphen)


Equivalent to a placeholder




Absolute size (text font)


Set the image to align based on the text baseline or bottom.




Relative size (text font)


Parent class, generally not used


Zooming based on X axis


Font Style: bold and italic


Subscript (used in mathematical formulas)


Superscript (used in mathematical formulas)


Text appearance (including font, size, style, and color)


Text font


Text hyperlink

The second and third parameters indicate the start position and end position of a text segment to be set to Span, namely start and end. The last parameter (flags) is a flag. You can also set the following values:

Spanned. SPAN_EXCLUSIVE_INCLUSIVE: The Span effect is not applied to the characters entered before the Span, and the Span effect is applied to the characters entered later.

Spanned. SPAN_INCLUSIVE_EXCLUSIVE: Apply the Span effect to the character entered before the Span, and do not apply the Span effect to the character entered after the Span.

Spanned. SPAN_INCUJSIVE_INCLUSIVE: Apply the Span effect to all the characters entered before and after the Span.

Application instance:

For details about SpannableString, refer to TextView advanced application for Android development.

Android Application Development details: what is written in this book? Which android sdk version is used in the book?

I have read that it is a very easy-to-understand book for beginners. The basic plug-in usage methods and various functions are well described and recommended.

Do you have any suggestions for registering a security officer during an interview with Foxconn?

Aquarius is both an alternative and good at being funny. When applying for and choosing a job, it will unconsciously work in the creative industry. Its novel and distinctive clothes may make these companies look strange and conform to the expectations of the other party. However, when talking about Aquarius, it is easy to fall into extreme ideas and comments, but it will scare the other party. It is recommended that Aquarius wear a balanced and perfect white crystal. Besides, white crystal, also known as Guan yunshi, can help your work!

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: 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.