Android Development notes-personalized TextView (Sina Weibo), Android textview
In the past few days, I have been writing a Sina Weibo client. When processing Weibo information, I have to deal with keyword highlighting and Weibo expressions. I have checked some information and decided to record something.
Let's take a look at the following:
For example, the # topic #, @ XXX nickname, HTTP: webpage links, and other elements are highlighted in blue on Weibo.
So how can we achieve these effects dynamically in our android program development?
In fact, it is very simple. I wrote a small example. Let's look at it first:
In fact, it is very simple to achieve this effect. In Android, a series of tool classes have been encapsulated for us, such:
Android. text. Spanned
Android. text. SpannableString
Android. text. SpannableStringBuilder
SpannableString and SpannableStringBuilder can be used to set different spans and can easily implement personalized textviews, such as bold, italic, foreground color, background color, font size, and font style. android. text. style. * defines many Span types for use.
In fact, there is nothing to say. This is just a tool class. You only need to master its general usage methods. Here, you can directly go to the Code (with a comment)
1 package com. example. spannabletest; 2 3 import java. util. hashMap; 4 import java. util. map; 5 import java. util. regex. matcher; 6 import java. util. regex. pattern; 7 8 import android. app. activity; 9 import android. graphics. color; 10 import android. graphics. drawable. drawable; 11 import android. OS. bundle; 12 import android. text. spannableString; 13 import android. text. spanned; 14 import android. text. style. foreg RoundColorSpan; 15 import android. text. style. imageSpan; 16 import android. text. style. relativeSizeSpan; 17 import android. widget. textView; 18 19 public class MainActivity extends Activity {20 21 private TextView textView; 22 23 // String to be converted 24 private String info = "# I love JAVA # My weibo Name: @ Balla _ rabbit [Rabbit] http://weibo.com/lichenwei1992 "; 25 26 @ Override27 protected void onCreate (Bundle savedInstanceState) {28 super. onCreat E (savedInstanceState); 29 setContentView (R. layout. activity_main); 30 textView = (TextView) findViewById (R. id. tx); 31 32 34 35/** 36 * provides many tool classes for personalized TextView content in Android. These classes can be used to replace regular strings. 37 * android. text. spanned38 * android. text. spannableString39 * android. text. spannableStringBuilder40 * 41 * Because Spannable and other classes finally implement the CharSequence interface, you can directly use SpannableString and SpannableStringBuilder through TextView. setText () is set to TextView. 42 */43 44 // instantiate a Spannable object 45 SpannableString spannableString = new SpannableString (info); 46 // use setSpan () method can be used to define different style content 47 // set the font 48 // Method 1: locate 49 spannableString directly. setSpan (new ForegroundColorSpan (Color. BLUE), 0, 8, Spanned. SPAN_EXCLUSIVE_EXCLUSIVE); 50 // Method 2: Use the String tool to locate 51 spannableString. setSpan (new ForegroundColorSpan (Color. BLUE), info. indexOf ("@"), info. indexOf (""), Spanned. SPAN_EXCLUSIVE_EXCLUSIVE); 52 // method 3: Use a regular expression to locate 53 Map <String, Integer> map = getHttpPostion (); 54 spannableString. setSpan (new ForegroundColorSpan (Color. BLUE), map. get ("start"), map. get ("end"), Spanned. SPAN_EXCLUSIVE_EXCLUSIVE); 55 56 // set the font size 57 spannableString. setSpan (new RelativeSizeSpan (float) 1.5), map. get ("start"), info. length (), Spanned. SPAN_EXCLUSIVE_EXCLUSIVE); 58 // set the image expression 59 Drawable drawable = getResources (). getDrawable (R. drawable. d_tuzi); 60 drawable. setBounds (0, 0, drawable. getIntrinsicWidth (), drawable. getIntrinsicHeight (); 61 spannableString. setSpan (new ImageSpan (drawable), info. indexOf ("["), info. indexOf ("]") + 1, Spanned. SPAN_INCLUSIVE_INCLUSIVE); 62 63 64 textView. setText (spannableString); 65 66 67} 68 69 public Map <String, Integer> getHttpPostion () {70 Map <String, Integer> map = new HashMap <String, integer> (); 71 Pattern pattern = Pattern. compile ("http :. * "); 72 Matcher matcher = pattern. matcher (info); 73 if (matcher. find () {74 map. put ("start", matcher. start (); 75 map. put ("end", matcher. end (); 76} 77 return map; 78 79} 80 81 82 83 84}
Usage:
1. To use a personalized TextView, we need to create a SpannableString or SpannableStringBuilder. The difference is that SpannableString is like a String, and a String is input when constructing an object, then, you cannot change the String content or splice multiple spannablestrings. The SpannableStringBuilder is more like a StringBuilder. It can splice multiple strings through its append () method.
SpannableString class:
SpannableStringBuffer class:
2. After creating Spannable objects, you can set the Span for them to achieve the desired personalization (SpannableString and SpannableStringBuilder both have the same Span method)
Here is the API:
Parameter 1: Object what (style here)
AbsoluteSizeSpan (int size): Set the font size. The parameter is an absolute value, which is equivalent to the font size in Word.
RelativeSizeSpan (float proportion): Set the font size. The parameter is a multiple of the default font size.
ScaleXSpan (float proportion): scales the font, which is similar to the preceding one. The default value is 1. After setting it, It is multiplied by proportion. If it is greater than 1, it is zoon in ), zoom out ).
BackgroundColorSpan (int color): Specifies the background Color. The parameter is a Color value. You can directly use the constant defined in android. graphics. color or use Color. rgb (int, int, int ).
ForegroundColorSpan (int color): Specifies the foreground color, that is, the color of the word. The parameter is consistent with that of the background color.
TypefaceSpan (String family): font. The parameter is the font name, such as "sans" and "sans-serif.
StyleSpan (Typeface style): font style, such as BOLD and ITALIC. The parameters are constants defined in android. graphics. Typeface, such as Typeface. BOLD and Typeface. ITALIC.
StrikethroughSpan: If this style is set, there will be a line passing through all words from the middle, just like being crossed out.
When using these Sytle spans, you can only pass the preceding constructor parameters. You do not need to set other attributes. If you need them, you can also set other attributes for them, for more information, see the API documentation.
Two and three parameters: (int start, int end)
Here refers to the location of personalized matching: there are many ways to implement it, such as writing a dead location directly, or using it with some methods of the String class, such as indexOf (), you can also write a regular expression matching method. If you want to match the regular expression multiple times, you can store the matching to a Map set. You can choose the matching method based on your project.
Parameter 4: (int flags)
Commonly used: (here, we can understand the definition of a range in mathematics, such as an open or closed interval)
Spanned. SPAN_EXCLUSIVE_EXCLUSIVE --- does not contain the endpoints of start and end at both ends
Spanned. SPAN_EXCLUSIVE_INCLUSIVE --- does not contain end start, but contains the end
Spanned. SPAN_INCLUSIVE_EXCLUSIVE --- contains the start at both ends, but does not include the endpoint of the end.
Spanned. SPAN_INCLUSIVE_INCLUSIVE --- contains the endpoints of start and end at both ends
There are other attributes, which will not be listed here. Let's go over the API documentation.
Let's look at the source code of SpannableString and SpannableStringBuffer. We can find that they all implement the CharSequence interface, so they can directly set it in TextView. setText ().
Well, here is just a reference, giving you the most basic usage methods. You need to flexibly change the usage methods in specific projects.