Android uses spannablestring to display compound text

Source: Internet
Author: User

http://blog.csdn.net/feizhixuan46789/article/details/10334441

Http://www.th7.cn/Program/Android/201408/258951.shtml

In the actual application development process often encountered, in different parts of the text display some different font style information such as: text font, size, color, style, and hyperlinks. In general, the text in TextView is a style, and for a similar scenario, you can use the Spannablestring or Spannablestringbuilder object to implement the above settings.

Spannablestring and Spannablestringbuilder can set a paragraph of text to a span, and in Android, span represents the effect of a piece of text, such as a link form, an image, text with a background, and so on. Just the difference is that Spannablestringbuilder can modify the contents of the text.

Ax, let's get acquainted with the Spannablestring class first.

Class Structure:

The content of such text is immutable, but the tagged object can be appended and detached. For mutable text, see Spannablestringbuilder. class overview:

Construction Method:

Public constructors

Spannablestring (charsequence Source)

Common public methods:

Public Methods

Final Char

charAt (int i)

Returns the character at the specified index, with the index of the first character at 0.

Boolean

Equals (Object o)

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

Compares this instance with the specified object, and returns True if it is equal.

Final void

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

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

Int

Getspanend (Object what)

Gets the end position of the what object in the Spannablestring object. If no match is returned-1.

Int

Getspanstart (Object what)

Gets the start position of the What object in the Spannablestring object. If no match is returned-1.

Final int

Length ()

Returns the number of characters in this sequence.

void

Removespan (Object what)

Removes the specified object from the range of text.

void

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

Sets a span object.

What

Description

void Setspan (Object what, int start, int end, int flags)

Sets a span object. The first parameter is what: represents the effect of a piece of text, which can accept the following properties:

Property name

Description

Backgroundcolorspan

Background color

Clickablespan

Text clickable, with click events

Foregroundcolorspan

Text colors (foreground color)

Maskfilterspan

Cosmetic effects, such as Blur (Blurmaskfilter) emboss (Embossmaskfilter)

Metricaffectingspan

Parent class, usually not used

Rasterizerspan

Grating effect

Strikethroughspan

Strikethrough (in dash)

Suggestionspan

Equivalent to a placeholder

Underlinespan

Underline

Absolutesizespan

Absolute size (text font)

Dynamicdrawablespan

Sets the picture, based on the text baseline or bottom alignment.

Imagespan

Image

Relativesizespan

Relative size (text font)

Replacementspan

Parent class, usually not used

Scalexspan

Scale based on X-axis

Stylespan

Font style: bold, italic, etc.

Subscriptspan

Subscript (The mathematical formula will be used)

Superscriptspan

Superscript (the mathematical formula will be used)

Textappearancespan

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

Typefacespan

Text font

Urlspan

Text hyperlinks

The second and third parameters represent the position of the beginning and the next character of the ending position of the text in which the text is to be set to span, that is, start and end. The last parameter (flags) is a flag. You can also set several similar values such as the following:

Spanned.span_exclusive_inclusive: The character entered in front of span does not apply the effect of span, and the span effect is applied to the characters entered later.

Spanned.span_inclusive_exclusive: The character entered before span applies the effect of span, and the character entered later does not apply a span effect.

Spanned.span_incujsive_inclusive: The effect of span is applied to characters entered before and after span.

Android uses spannablestring to display compound text

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.