Detailed explanation of spannablestring and spannablestringbuilder in Android

preface

Recently, I was studying Android development and found that there are really too many things to sort out and learn. Take your time. Any in-depth study is not easy. Today, let's sort out spannablestring and spannablestringbuilder. In the previous article, we talked about charsequence. These two classes are necessary to talk about charsequence, so we'll sort them out here.

1、 Overview

1. Spannablestring, relationship between spannablestringbuilder and string

Firstly, spannablestring and spannablestringbuilder are basically similar to strings and are also used to store strings, but their special feature is that they have a setspan() function, which can add various formats or styles (span) to these stored strings and display the original strings in different styles, such as underlining, background color Change the font color, replace the specified text with a picture, and so on. Therefore, in a word, spannablestring and spannablestringbuilder, like string, first pass strings, but spannablestring and spannablestringbuilder can add additional style information to these strings, but string cannot.

Note: if these additional information can be supported by the method used, such as passing spannablestring to textview; There are also those that do not support these additional information, such as canvas drawing text mentioned in the previous chapter. For those that do not support it, spannablestring and spannablestringbuilder degenerate to string type and directly display the original string string instead of these additional additional information.

2. What is the difference between spannablestring and spannablestringbuilder

The difference between them is that spannablestring is like a string. When constructing an object, a string is passed in, and then the content of the string cannot be changed, nor can multiple spannablestrings be spliced; Spannablestringbuilder is more like a StringBuilder. It can splice multiple strings through its append() method:

(posted from blog: Android - spannablestring or spannablestringbuilder and integer and string substitution in string.xml file)

Because spannable and others finally implement the charsequence interface, spannablestring and spannablestringbuilder can be directly set to textview through textview. Settext().

3、SetSpan()

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

Function meaning: set span styles for strings in a specific range of spannablestring or spannablestringbuilder. You can set multiple styles (such as adding underline and strikeout at the same time). The falg parameter identifies the action when inserting new characters immediately before and after the marked range, that is, whether to apply the same style to the newly inserted characters. (examples will be given later)

Parameter Description:

Int flags: there are four values:

Take an example to illustrate the problems before and after this:

Since flag is used to specify whether the effect will be applied when new characters are entered before and after the range, we use EditText to display spannablestring

(1) . add an EditText control to the layout XML:

(2) Here, a span that changes the font color is used for the next demonstration

The initialization effect is as follows:

Add the word shrimp in the front and back. It can be seen that the shrimp in the front has no effect, but the latter is different. Add the same span special effect, because we set spannable.span_ EXCLUSIVE_ The reason for inclusive, that is (special effects are not applied in the front and special effects are applied in the back), the meaning of the other flags parameters must be clear to everyone. I won't repeat it here.

2、 Various span settings

In the previous small example, you can also see that there are three steps to apply a span:

1. Construct string

2. Construct a span 3. Use setspan() to apply the span to the string in the specified range

1. Font color setting (foreroundcolorspan)

effect:

3. Font size (absolutesizespan)

4. Bold, italic (stylespan)

5. Strikethrough span

6. Underline (underlinespan)

7. Image replacement (imagespan)

Imagespan has many constructors, which are generally constructed by passing drawableg. See here for detailed construction instructions: http://developer.android.com/reference/android/text/style/ImageSpan.html

The difference of this function is that in the first few, special effects are added on the basis of the original text, but here the text is replaced with pictures. If you encounter a function that does not support displaying pictures, such as canvas drawing in the previous article. It will degenerate into a string, that is, it will be displayed as the original string.

Download address of pictures and projects involved in this article: http://xiazai.jb51.net/201710/yuanma/SpannableString_ SpannableStringBuilder(jb51.net).rar

summary

The above is the whole content of this article. I hope the content of this article has a certain reference value for your study or work. If you have any questions, you can leave a message. Thank you for your support for programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>