Android Development Notes – personalized textview (Sina Weibo)

In the past few days, I have been imitating Sina Weibo client. When processing Weibo information, I need to deal with keyword highlighting and Weibo expression. I checked some data and decided to record something

Let's take a look at the renderings:

Like the above # topics #, @ XXX nickname, http: Web page links and other elements are highlighted in blue in microblog.

So how should we dynamically achieve these effects in our Android program development?

Actually, it's very simple. I wrote a small example. Let's take a look at the renderings:

In fact, it is very simple to achieve this effect. A series of tool classes have been encapsulated in Android, such as:

android. text. Spanned

android. text. SpannableString

android. text. SpannableStringBuilder

Spannablestring and spannablestringbuilder can be used to set different spans and easily implement personalized textviews, such as bold, italic, foreground, background color, font size, font style, etc. Android text. style.* There are many span types defined in and available for use.

In fact, there's nothing to say. It's just a tool class. You only need to master its general usage methods. Here you can go directly to the code (with comments)

usage method:

1. To use personalized textview, we need to create a spannablestring or spannablestringbuilder. The difference 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 or multiple spannablestrings can not be spliced; Spannablestringbuilder is more like a StringBuilder, which can splice multiple strings through its append () method.

Spannablestring class:

Spannablestringbuffer class:

2. After creating spannable objects, you can set span for them to achieve the desired personalization (spannablestring and spannablestringbuilder have the same span method)

Here is the API:

Parameter 1: object what (here refers to style)

Absolutesizespan (int size): sets the font size. The parameter is an absolute value, which is equivalent to the font size in word.

Relativesizespan (float proportion): sets the font size. The parameter is a multiple of the default font size.

Scalexspan (float proportion): scale the font, which is similar to the above. The default value is 1. After setting, the original value is multiplied by the proportion. When it is greater than 1, zoom in and zoom out.

Backgroundcolorspan (int color): background coloring. The parameter is the color value. You can directly use Android graphics. The constant defined in color, or color rgb(int,int,int)。

Foregroundcolorspan (int color): foreground coloring, that is, the coloring of words. The parameters are consistent with the background coloring.

Typefacespan (string family): font. The parameter is the name of the font, such as "sans", "sans serif", etc.

Stylespan (typeface style): font style, such as bold, italic, and the parameter is Android graphics. Constants defined in typeface, such as typeface BOLD,Typeface. Italic, etc.

Strike through span: if this style is set, a line will pass through all words from the middle, as if it were crossed out.

When using these systle spans, you usually only need to pass the construction parameters described above. There is no need to set other properties. If necessary, you can also set other properties for them. For details, please refer to the API document.

Parameters two and three: (int start, int end)

Here refers to the location of personalized matching: there are many ways to implement it. For example, write the dead location directly, or use it in conjunction with some methods of string class, such as indexof (), or write a regular matching method. If you want to match multiple times, you can store these matches in a map set. According to the specific situation, you can choose according to your own project.

Parameter 4: (int flags)

Common are: (it's like the definition of interval in mathematics, open interval or closed interval)

  Spanned. SPAN_ EXCLUSIVE_ Exclusive --- excluding the endpoints where start and end at both ends are located

  Spanned. SPAN_ EXCLUSIVE_ Inclusive --- does not include the end start, but the end where the end is located

  Spanned. SPAN_ INCLUSIVE_ Exclusive --- includes start at both ends, but does not include the endpoint where end is located

  Spanned. SPAN_ INCLUSIVE_ Inclusive --- contains the endpoints where start and end are located at both ends

There are some other attributes, which are not listed here one by one. Let's look at the API documents ourselves.

Let's look at the source code of spannablestring and spannablestringbuffer. We can find that both of them implement the charsequence interface, so they can be directly in textview Set in settext()

Well, here is just a brick to attract jade. The most basic use methods are given. Flexible use methods need to be changed in specific projects.

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
分享
二维码
< <上一篇
下一篇>>