- 浏览: 1421771 次
文章分类
最新评论
-
学习的小麦:
博主是个大好人,求求你了1169293832我QQ,先谢博主
局域网控制(二)——控制客户端 -
学习的小麦:
博主,由于毕业设计的需要,可以请求博主给完整程序给我吗?
局域网控制(二)——控制客户端 -
netkongjian:
不错CAD控件知识,感谢分享[deyi]
适合任何CAD版本的CAD开发技巧 -
wangguxian:
它的起点是没有算在里面的,所以第一个点的返回值是-1,其他的都 ...
和大家分享一个判断点是否在多边形范围内的算法 -
xchyou:
ECMAScript
【郭林专刊】Android实战技巧:用TextView实现Rich Text---在同一个TextView中设置不同的字体风格
背景介绍
在开发应用过程中经常会遇到显示一些不同的字体风格的信息犹如默认的LockScreen上面的时间和充电信息。对于类似的情况,可能第一反应就是用不同的多个TextView来实现,对于每个TextView设置不同的字体风格以满足需求。
这里推荐的做法是使用android.text.*;和android.text.style.*;下面的组件来实现RichText:也即在同一个TextView中设置不同的字体风格。对于某些应用,比如文本编辑,记事本,彩信,短信等地方,还必须使用这些组件才能达到想到的显示效果。
主要的基本工具类有android.text.Spanned; android.text.SpannableString; android.text.SpannableStringBuilder;使用这些类来代替常规String。SpannableString和SpannableStringBuilder可以用来设置不同的Span,这些Span便是用于实现Rich Text,比如粗体,斜体,前景色,背景色,字体大小,字体风格等等,android.text.style.*中定义了很多的Span类型可供使用。
这是相关的API的Class General Hierarchy:
因为Spannable等最终都实现了CharSequence接口,所以可以直接把SpannableString和SpannableStringBuilder通过TextView.setText()设置给TextView。
使用方法
当要显示Rich Text信息的时候,可以使用创建一个SpannableString或SpannableStringBuilder,它们的区别在于SpannableString像一个String一样,构造对象的时候传入一个String,之后再无法更改String的内容,也无法拼接多个SpannableString;而SpannableStringBuilder则更像是StringBuilder,它可以通过其append()方法来拼接多个String:
- SpannableStringword=newSpannableString("Thequickfoxjumpsoverthelazydog");
- SpannableStringBuildermultiWord=newSpannableStringBuilder();
- multiWord.append("TheQuickFox");
- multiWord.append("jumpsover");
- multiWord.append("thelazydog");
- AbsoluteSizeSpan(int size) ---- 设置字体大小,参数是绝对数值,相当于Word中的字体大小
- RelativeSizeSpan(float proportion) ---- 设置字体大小,参数是相对于默认字体大小的倍数,比如默认字体大小是x, 那么设置后的字体大小就是x*proportion,这个用起来比较灵活,proportion>1就是放大(zoom in), proportion<1就是缩小(zoom out)
- ScaleXSpan(float proportion) ---- 缩放字体,与上面的类似,默认为1,设置后就是原来的乘以proportion,大于1时放大(zoon in),小于时缩小(zoom out)
- BackgroundColorSpan(int color) ----背景着色,参数是颜色数值,可以直接使用android.graphics.Color里面定义的常量,或是用Color.rgb(int, int, int)
- ForegroundColorSpan(int color) ----前景着色,也就是字的着色,参数与背景着色一致
- TypefaceSpan(String family) ----字体,参数是字体的名字比如“sans", "sans-serif"等
- StyleSpan(Typeface style) -----字体风格,比如粗体,斜体,参数是android.graphics.Typeface里面定义的常量,如Typeface.BOLD,Typeface.ITALIC等等。
- StrikethroughSpan----如果设置了此风格,会有一条线从中间穿过所有的字,就像被划掉一样
SpannableString和SpannableStringBuilder都有一个设置上述Span的方法:
- /**
- *SetthestylespantoSpannable,suchasSpannableStringorSpannableStringBuilder
- *@paramwhat---thestylespan,suchasStyleSpan
- *@paramstart---thestartingindexofcharacterstowhichthestylespantoapply
- *@paramend---theendingindexofcharacterstowhichthestylespantoapply
- *@paramflags---theflagspecifiedtocontrol
- */
- setSpan(Objectwhat,intstart,intend,intflags);
其中参数what是要设置的Style span,start和end则是标识String中Span的起始位置,而 flags是用于控制行为的,通常设置为0或Spanned中定义的常量,常用的有:
- Spanned.SPAN_EXCLUSIVE_EXCLUSIVE --- 不包含两端start和end所在的端点
- Spanned.SPAN_EXCLUSIVE_INCLUSIVE --- 不包含端start,但包含end所在的端点
- Spanned.SPAN_INCLUSIVE_EXCLUSIVE --- 包含两端start,但不包含end所在的端点
- Spanned.SPAN_INCLUSIVE_INCLUSIVE--- 包含两端start和end所在的端点
权衡选择
个人认为软件开发中最常见的问题不是某个技巧怎么使用的问题,而是何时该使用何技巧的问题,因为实现同一个目标可能有N种不同的方法,就要权衡利弊,选择最合适的一个,正如常言所云,没有最好的,只有最适合的。如前面所讨论的,要想用不同的字体展现不同的信息可能的解法,除了用Style Span外还可以用多个TextView。那么就需要总结下什么时候该使用StyleSpan,什么时候该使用多个TextView:
- 如果显示的是多个不同类别的信息,就应该使用多个TextView,这样也方便控制和改变各自的信息,例子就是默认LockScreen上面的日期和充电信息,因为它们所承载不同的信息,所以应该使用多个TextView来分别呈现。
- 如果显示的是同一类信息,或者同一个信息,那么应该使用StyleSpan。比如,短信息中,要把联系人的相关信息突出显示;或是想要Highlight某些信息等。
- 如果要实现Rich text,没办法,只能使用Style span。
- 如果要实现某些特效,也可以考虑使用StyleSpan。设置不同的字体风格只是Style span的初级应用,如果深入研究,可以发现很多奇妙的功效。
实例
phone number, web address or email address,并标识为超链接,可点击,点击后便跳转到相应的应用,如Dialer,Browser或Email。
- <?xmlversion="1.0"encoding="utf-8"?>
- <LinearLayout
- xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:orientation="vertical">
- <ScrollView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content">
- <LinearLayout
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:orientation="vertical">
- <TextView
- android:id="@+id/text_view_font_1"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- />
- <TextView
- android:id="@+id/text_view_font_2"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- />
- <TextView
- android:id="@+id/text_view_font_3"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- />
- <TextView
- android:id="@+id/text_view_font_4"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- />
- <TextView
- android:id="@+id/text_view_font_5"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- />
- </LinearLayout>
- </ScrollView>
- </LinearLayout>
- packagecom.android.effective;
- importjava.util.regex.Matcher;
- importjava.util.regex.Pattern;
- importandroid.app.Activity;
- importandroid.graphics.Color;
- importandroid.graphics.Typeface;
- importandroid.os.Bundle;
- importandroid.text.Spannable;
- importandroid.text.SpannableString;
- importandroid.text.SpannableStringBuilder;
- importandroid.text.style.AbsoluteSizeSpan;
- importandroid.text.style.BackgroundColorSpan;
- importandroid.text.style.ForegroundColorSpan;
- importandroid.text.style.QuoteSpan;
- importandroid.text.style.RelativeSizeSpan;
- importandroid.text.style.ScaleXSpan;
- importandroid.text.style.StrikethroughSpan;
- importandroid.text.style.StyleSpan;
- importandroid.text.style.TypefaceSpan;
- importandroid.text.style.URLSpan;
- importandroid.text.util.Linkify;
- importandroid.widget.TextView;
- publicclassTextViewFontActivityextendsActivity{
- @Override
- publicvoidonCreate(Bundlebundle){
- super.onCreate(bundle);
- setContentView(R.layout.textview_font_1);
- //DemonstrationofbasicSpannableStringandspansusage
- finalTextViewtextWithString=(TextView)findViewById(R.id.text_view_font_1);
- Stringw="Thequickfoxjumpsoverthelazydog";
- intstart=w.indexOf('q');
- intend=w.indexOf('k')+1;
- Spannableword=newSpannableString(w);
- word.setSpan(newAbsoluteSizeSpan(22),start,end,
- Spannable.SPAN_INCLUSIVE_INCLUSIVE);
- word.setSpan(newStyleSpan(Typeface.BOLD),start,end,
- Spannable.SPAN_INCLUSIVE_INCLUSIVE);
- word.setSpan(newBackgroundColorSpan(Color.RED),start,end,
- Spannable.SPAN_INCLUSIVE_INCLUSIVE);
- textWithString.setText(word);
- //DemonstrationofbasicSpannableStringBuilderandspansusage
- finalTextViewtextWithBuilder=(TextView)findViewById(R.id.text_view_font_2);
- SpannableStringBuilderword2=newSpannableStringBuilder();
- finalStringone="Freedomisnothingbutachancetobebetter!";
- finalStringtwo="Thequickfoxjumpsoverthelazydog!";
- finalStringthree="Thetreeoflibertymustberefreshedfromtimetotimewith"+
- "thebloodofpatroitsandtyrants!";
- word2.append(one);
- start=0;
- end=one.length();
- word2.setSpan(newStyleSpan(Typeface.BOLD_ITALIC),start,end,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
- word2.append(two);
- start=end;
- end+=two.length();
- word2.setSpan(newForegroundColorSpan(Color.CYAN),start,end,
- Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
- word2.append(three);
- start=end;
- end+=three.length();
- word2.setSpan(newURLSpan(three),start,end,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
- textWithBuilder.setText(word2);
- //TroubleshootingwhenusingSpannableStringBuilder
- finalTextViewtextTroubles=(TextView)findViewById(R.id.text_view_font_3);
- SpannableStringBuilderword3=newSpannableStringBuilder();
- start=0;
- end=one.length();
- //Caution:mustfirstappendorsettexttoSpannableStringBuilderorSpannableString
- //thensetthespanstothem,otherwise,IndexOutOfBoundExceptionisthrownwhensettingspans
- word3.append(one);
- //ForAbsoluteSizeSpan,theflagmustbesetto0,otherwise,itwillapplythisspantountilendoftext
- word3.setSpan(newAbsoluteSizeSpan(22),start,end,0);//Spannable.SPAN_INCLUSIVE_INCLUSIVE);
- //ForBackgroundColorSpanSpan,theflagmustbesetto0,otherwise,itwillapplythisspantoendoftext
- word3.setSpan(newBackgroundColorSpan(Color.DKGRAY),start,end,0);//Spannable.SPAN_INCLUSIVE_INCLUSIVE);
- word3.append(two);
- start=end;
- end+=two.length();
- word3.setSpan(newTypefaceSpan("sans-serif"),start,end,
- Spannable.SPAN_INCLUSIVE_INCLUSIVE);
- //TODO:sometimes,flagmustbesetto0,otherwiseitwillapplythespantountilendoftext
- //whichMIGHThasnothingtodowithspecificspantype.
- word3.setSpan(newStyleSpan(Typeface.BOLD_ITALIC),start,end,0);//Spannable.SPAN_INCLUSIVE_INCLUSIVE);
- word3.setSpan(newScaleXSpan(0.618f),start,end,Spannable.SPAN_INCLUSIVE_INCLUSIVE);
- word3.setSpan(newStrikethroughSpan(),start,end,0);//Spannable.SPAN_INCLUSIVE_INCLUSIVE);
- word3.setSpan(newForegroundColorSpan(Color.CYAN),start,end,Spannable.SPAN_INCLUSIVE_INCLUSIVE);
- word3.setSpan(newQuoteSpan(),start,end,0);//Spannable.SPAN_INCLUSIVE_INCLUSIVE);
- word3.append(three);
- start=end;
- end+=three.length();
- word3.setSpan(newRelativeSizeSpan((float)Math.E),start,end,Spannable.SPAN_INCLUSIVE_INCLUSIVE);
- word3.setSpan(newForegroundColorSpan(Color.BLUE),start,end,Spannable.SPAN_INCLUSIVE_INCLUSIVE);
- textTroubles.setText(word3);
- //Highlightsomepatterns
- finalStringfour="Thegapbetweenthebestsoftwareengineering"+
- "practiceandtheaveragepracticeisverywide¡ªperhapswider"+
- "thaninanyotherengineeringdiscipline.Atoolthatdisseminates"+
- "goodpracticewouldbeimportant.¡ªFredBrooks";
- finalPatternhighlight=Pattern.compile("the");
- finalTextViewtextHighlight=(TextView)findViewById(R.id.text_view_font_4);
- SpannableStringword4=newSpannableString(four);
- Matcherm=highlight.matcher(word4.toString());
- while(m.find()){
- word4.setSpan(newStyleSpan(Typeface.BOLD_ITALIC),m.start(),m.end(),
- Spannable.SPAN_INCLUSIVE_INCLUSIVE);
- word4.setSpan(newForegroundColorSpan(Color.RED),m.start(),m.end(),
- Spannable.SPAN_INCLUSIVE_INCLUSIVE);
- word4.setSpan(newStrikethroughSpan(),m.start(),m.end(),
- Spannable.SPAN_INCLUSIVE_INCLUSIVE);
- }
- textHighlight.setText(word4);
- //Setnumbers,URLsandE-mailaddresstobeclickablewithTextView#setAutoLinkMask
- finalTextViewtextClickable=(TextView)findViewById(R.id.text_view_font_5);
- finalStringcontact="Email:mvp@microsoft.com\n"+
- "Phone:+47-24885883\n"+
- "Fax:+47-24885883\n"+
- "HTTP:www.microsoft.com/mvp.asp";
- //Settheattributefirst,thensetthetext.Otherwise,itwon'twork
- textClickable.setAutoLinkMask(Linkify.ALL);//orset'android:autoLink'inlayoutxml
- textClickable.setText(contact);
- }
- }
背景介绍
在开发应用过程中经常会遇到显示一些不同的字体风格的信息犹如默认的LockScreen上面的时间和充电信息。对于类似的情况,可能第一反应就是用不同的多个TextView来实现,对于每个TextView设置不同的字体风格以满足需求。
这里推荐的做法是使用android.text.*;和android.text.style.*;下面的组件来实现RichText:也即在同一个TextView中设置不同的字体风格。对于某些应用,比如文本编辑,记事本,彩信,短信等地方,还必须使用这些组件才能达到想到的显示效果。
主要的基本工具类有android.text.Spanned; android.text.SpannableString; android.text.SpannableStringBuilder;使用这些类来代替常规String。SpannableString和SpannableStringBuilder可以用来设置不同的Span,这些Span便是用于实现Rich Text,比如粗体,斜体,前景色,背景色,字体大小,字体风格等等,android.text.style.*中定义了很多的Span类型可供使用。
这是相关的API的Class General Hierarchy:
因为Spannable等最终都实现了CharSequence接口,所以可以直接把SpannableString和SpannableStringBuilder通过TextView.setText()设置给TextView。
使用方法
当要显示Rich Text信息的时候,可以使用创建一个SpannableString或SpannableStringBuilder,它们的区别在于SpannableString像一个String一样,构造对象的时候传入一个String,之后再无法更改String的内容,也无法拼接多个SpannableString;而SpannableStringBuilder则更像是StringBuilder,它可以通过其append()方法来拼接多个String:
- SpannableStringword=newSpannableString("Thequickfoxjumpsoverthelazydog");
- SpannableStringBuildermultiWord=newSpannableStringBuilder();
- multiWord.append("TheQuickFox");
- multiWord.append("jumpsover");
- multiWord.append("thelazydog");
- AbsoluteSizeSpan(int size) ---- 设置字体大小,参数是绝对数值,相当于Word中的字体大小
- RelativeSizeSpan(float proportion) ---- 设置字体大小,参数是相对于默认字体大小的倍数,比如默认字体大小是x, 那么设置后的字体大小就是x*proportion,这个用起来比较灵活,proportion>1就是放大(zoom in), proportion<1就是缩小(zoom out)
- ScaleXSpan(float proportion) ---- 缩放字体,与上面的类似,默认为1,设置后就是原来的乘以proportion,大于1时放大(zoon in),小于时缩小(zoom out)
- BackgroundColorSpan(int color) ----背景着色,参数是颜色数值,可以直接使用android.graphics.Color里面定义的常量,或是用Color.rgb(int, int, int)
- ForegroundColorSpan(int color) ----前景着色,也就是字的着色,参数与背景着色一致
- TypefaceSpan(String family) ----字体,参数是字体的名字比如“sans", "sans-serif"等
- StyleSpan(Typeface style) -----字体风格,比如粗体,斜体,参数是android.graphics.Typeface里面定义的常量,如Typeface.BOLD,Typeface.ITALIC等等。
- StrikethroughSpan----如果设置了此风格,会有一条线从中间穿过所有的字,就像被划掉一样
SpannableString和SpannableStringBuilder都有一个设置上述Span的方法:
- /**
- *SetthestylespantoSpannable,suchasSpannableStringorSpannableStringBuilder
- *@paramwhat---thestylespan,suchasStyleSpan
- *@paramstart---thestartingindexofcharacterstowhichthestylespantoapply
- *@paramend---theendingindexofcharacterstowhichthestylespantoapply
- *@paramflags---theflagspecifiedtocontrol
- */
- setSpan(Objectwhat,intstart,intend,intflags);
其中参数what是要设置的Style span,start和end则是标识String中Span的起始位置,而 flags是用于控制行为的,通常设置为0或Spanned中定义的常量,常用的有:
- Spanned.SPAN_EXCLUSIVE_EXCLUSIVE --- 不包含两端start和end所在的端点
- Spanned.SPAN_EXCLUSIVE_INCLUSIVE --- 不包含端start,但包含end所在的端点
- Spanned.SPAN_INCLUSIVE_EXCLUSIVE --- 包含两端start,但不包含end所在的端点
- Spanned.SPAN_INCLUSIVE_INCLUSIVE--- 包含两端start和end所在的端点
权衡选择
个人认为软件开发中最常见的问题不是某个技巧怎么使用的问题,而是何时该使用何技巧的问题,因为实现同一个目标可能有N种不同的方法,就要权衡利弊,选择最合适的一个,正如常言所云,没有最好的,只有最适合的。如前面所讨论的,要想用不同的字体展现不同的信息可能的解法,除了用Style Span外还可以用多个TextView。那么就需要总结下什么时候该使用StyleSpan,什么时候该使用多个TextView:
- 如果显示的是多个不同类别的信息,就应该使用多个TextView,这样也方便控制和改变各自的信息,例子就是默认LockScreen上面的日期和充电信息,因为它们所承载不同的信息,所以应该使用多个TextView来分别呈现。
- 如果显示的是同一类信息,或者同一个信息,那么应该使用StyleSpan。比如,短信息中,要把联系人的相关信息突出显示;或是想要Highlight某些信息等。
- 如果要实现Rich text,没办法,只能使用Style span。
- 如果要实现某些特效,也可以考虑使用StyleSpan。设置不同的字体风格只是Style span的初级应用,如果深入研究,可以发现很多奇妙的功效。
实例
phone number, web address or email address,并标识为超链接,可点击,点击后便跳转到相应的应用,如Dialer,Browser或Email。
- <?xmlversion="1.0"encoding="utf-8"?>
- <LinearLayout
- xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:orientation="vertical">
- <ScrollView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content">
- <LinearLayout
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:orientation="vertical">
- <TextView
- android:id="@+id/text_view_font_1"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- />
- <TextView
- android:id="@+id/text_view_font_2"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- />
- <TextView
- android:id="@+id/text_view_font_3"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- />
- <TextView
- android:id="@+id/text_view_font_4"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- />
- <TextView
- android:id="@+id/text_view_font_5"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- />
- </LinearLayout>
- </ScrollView>
- </LinearLayout>
- packagecom.android.effective;
- importjava.util.regex.Matcher;
- importjava.util.regex.Pattern;
- importandroid.app.Activity;
- importandroid.graphics.Color;
- importandroid.graphics.Typeface;
- importandroid.os.Bundle;
- importandroid.text.Spannable;
- importandroid.text.SpannableString;
- importandroid.text.SpannableStringBuilder;
- importandroid.text.style.AbsoluteSizeSpan;
- importandroid.text.style.BackgroundColorSpan;
- importandroid.text.style.ForegroundColorSpan;
- importandroid.text.style.QuoteSpan;
- importandroid.text.style.RelativeSizeSpan;
- importandroid.text.style.ScaleXSpan;
- importandroid.text.style.StrikethroughSpan;
- importandroid.text.style.StyleSpan;
- importandroid.text.style.TypefaceSpan;
- importandroid.text.style.URLSpan;
- importandroid.text.util.Linkify;
- importandroid.widget.TextView;
- publicclassTextViewFontActivityextendsActivity{
- @Override
- publicvoidonCreate(Bundlebundle){
- super.onCreate(bundle);
- setContentView(R.layout.textview_font_1);
- //DemonstrationofbasicSpannableStringandspansusage
- finalTextViewtextWithString=(TextView)findViewById(R.id.text_view_font_1);
- Stringw="Thequickfoxjumpsoverthelazydog";
- intstart=w.indexOf('q');
- intend=w.indexOf('k')+1;
- Spannableword=newSpannableString(w);
- word.setSpan(newAbsoluteSizeSpan(22),start,end,
- Spannable.SPAN_INCLUSIVE_INCLUSIVE);
- word.setSpan(newStyleSpan(Typeface.BOLD),start,end,
- Spannable.SPAN_INCLUSIVE_INCLUSIVE);
- word.setSpan(newBackgroundColorSpan(Color.RED),start,end,
- Spannable.SPAN_INCLUSIVE_INCLUSIVE);
- textWithString.setText(word);
- //DemonstrationofbasicSpannableStringBuilderandspansusage
- finalTextViewtextWithBuilder=(TextView)findViewById(R.id.text_view_font_2);
- SpannableStringBuilderword2=newSpannableStringBuilder();
- finalStringone="Freedomisnothingbutachancetobebetter!";
- finalStringtwo="Thequickfoxjumpsoverthelazydog!";
- finalStringthree="Thetreeoflibertymustberefreshedfromtimetotimewith"+
- "thebloodofpatroitsandtyrants!";
- word2.append(one);
- start=0;
- end=one.length();
- word2.setSpan(newStyleSpan(Typeface.BOLD_ITALIC),start,end,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
- word2.append(two);
- start=end;
- end+=two.length();
- word2.setSpan(newForegroundColorSpan(Color.CYAN),start,end,
- Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
- word2.append(three);
- start=end;
- end+=three.length();
- word2.setSpan(newURLSpan(three),start,end,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
- textWithBuilder.setText(word2);
- //TroubleshootingwhenusingSpannableStringBuilder
- finalTextViewtextTroubles=(TextView)findViewById(R.id.text_view_font_3);
- SpannableStringBuilderword3=newSpannableStringBuilder();
- start=0;
- end=one.length();
- //Caution:mustfirstappendorsettexttoSpannableStringBuilderorSpannableString
- //thensetthespanstothem,otherwise,IndexOutOfBoundExceptionisthrownwhensettingspans
- word3.append(one);
- //ForAbsoluteSizeSpan,theflagmustbesetto0,otherwise,itwillapplythisspantountilendoftext
- word3.setSpan(newAbsoluteSizeSpan(22),start,end,0);//Spannable.SPAN_INCLUSIVE_INCLUSIVE);
- //ForBackgroundColorSpanSpan,theflagmustbesetto0,otherwise,itwillapplythisspantoendoftext
- word3.setSpan(newBackgroundColorSpan(Color.DKGRAY),start,end,0);//Spannable.SPAN_INCLUSIVE_INCLUSIVE);
- word3.append(two);
- start=end;
- end+=two.length();
- word3.setSpan(newTypefaceSpan("sans-serif"),start,end,
- Spannable.SPAN_INCLUSIVE_INCLUSIVE);
- //TODO:sometimes,flagmustbesetto0,otherwiseitwillapplythespantountilendoftext
- //whichMIGHThasnothingtodowithspecificspantype.
- word3.setSpan(newStyleSpan(Typeface.BOLD_ITALIC),start,end,0);//Spannable.SPAN_INCLUSIVE_INCLUSIVE);
- word3.setSpan(newScaleXSpan(0.618f),start,end,Spannable.SPAN_INCLUSIVE_INCLUSIVE);
- word3.setSpan(newStrikethroughSpan(),start,end,0);//Spannable.SPAN_INCLUSIVE_INCLUSIVE);
- word3.setSpan(newForegroundColorSpan(Color.CYAN),start,end,Spannable.SPAN_INCLUSIVE_INCLUSIVE);
- word3.setSpan(newQuoteSpan(),start,end,0);//Spannable.SPAN_INCLUSIVE_INCLUSIVE);
- word3.append(three);
- start=end;
- end+=three.length();
- word3.setSpan(newRelativeSizeSpan((float)Math.E),start,end,Spannable.SPAN_INCLUSIVE_INCLUSIVE);
- word3.setSpan(newForegroundColorSpan(Color.BLUE),start,end,Spannable.SPAN_INCLUSIVE_INCLUSIVE);
- textTroubles.setText(word3);
- //Highlightsomepatterns
- finalStringfour="Thegapbetweenthebestsoftwareengineering"+
- "practiceandtheaveragepracticeisverywide¡ªperhapswider"+
- "thaninanyotherengineeringdiscipline.Atoolthatdisseminates"+
- "goodpracticewouldbeimportant.¡ªFredBrooks";
- finalPatternhighlight=Pattern.compile("the");
- finalTextViewtextHighlight=(TextView)findViewById(R.id.text_view_font_4);
- SpannableStringword4=newSpannableString(four);
- Matcherm=highlight.matcher(word4.toString());
- while(m.find()){
- word4.setSpan(newStyleSpan(Typeface.BOLD_ITALIC),m.start(),m.end(),
- Spannable.SPAN_INCLUSIVE_INCLUSIVE);
- word4.setSpan(newForegroundColorSpan(Color.RED),m.start(),m.end(),
- Spannable.SPAN_INCLUSIVE_INCLUSIVE);
- word4.setSpan(newStrikethroughSpan(),m.start(),m.end(),
- Spannable.SPAN_INCLUSIVE_INCLUSIVE);
- }
- textHighlight.setText(word4);
- //Setnumbers,URLsandE-mailaddresstobeclickablewithTextView#setAutoLinkMask
- finalTextViewtextClickable=(TextView)findViewById(R.id.text_view_font_5);
- finalStringcontact="Email:mvp@microsoft.com\n"+
- "Phone:+47-24885883\n"+
- "Fax:+47-24885883\n"+
- "HTTP:www.microsoft.com/mvp.asp";
- //Settheattributefirst,thensetthetext.Otherwise,itwon'twork
- textClickable.setAutoLinkMask(Linkify.ALL);//orset'android:autoLink'inlayoutxml
- textClickable.setText(contact);
- }
- }
相关推荐
仿照郭林博客http://blog.csdn.net/guolin_blog/article/details/34093441写的demo,androidStudio版本,他下载的链接是eclipse的代码,在高版本的AndroidStudio运行会不方便
android中数据的更新需要用户很方便就能操作,其中下拉刷新就是很好的一种用户体验方式,这是郭林大神在网上的一个下拉刷新的demo,我做了一点小小的修改,然后加了一下注释,方便大家参考。
Android大屏幕适配demo,这个是仿照郭林文章中的例子,他的例子是用eclipse写的,直接运行会不兼容最新的sdk,我改成了AndroidStudio可以直接运行的demo.郭林此代码文章的连接 ...
单分散性钴铁氧体纳米粒子的研究进展,王志成,郭林,钴铁氧体纳米磁性粒子具有良好的物理化学性能,因此其相关合成备受关注。本文总结了钴铁氧体纳米粒子的制备方法,如化学共沉淀法
学习郭林老师的见证消息推送时刻笔记,笔记未整理有点乱,有很多不足,希望大家指点,源代码随后上传
采用改进高温热分解法合成了Cd2+掺杂NaLuF4∶Yb, Er纳米晶体,研究了Cd2+对晶相形成和发光强度的影响,采用CASTEP计算不同掺杂浓度下β-NaLuF4∶Yb, Er的形成能。在掺杂浓度为6mol%时合成的纳米晶荧光强度最强,其相较...
这个是参考网上的资料,结合google官方的文档写的一个volley的简单demo,并做了简单的封装,想更加系统的学习volley可以参考google官方文档以及郭林的博客
基于评论对象的新闻影响力预测算法,魏忠祥,郭林,近年来网络媒体对社会的影响力越来越大,能够定量地分析预测新闻事件的影响力显得尤为重要。目前一些研究都只是针对新闻的点击量
丝网状镍纳米材料的合成与磁学性质研究,周苇,郭林,以高聚物为软模板,利用温和的湿化学法合成了丝网状镍纳米材料。利用扫描电镜、高分辨透射电镜及X射线衍射仪对样品进行了表征分�
温度应力下海底管线水平屈曲分析,刘润,郭林坪,温度应力下海底管线的屈曲大变形是管线设计中所要考虑的关键问题之一。以某实际工程为背景,应用理想平直管线发生水平向屈曲的解
小波去噪的低信噪比直扩信号扩频码盲估计算法,郭林,吕明,本文针对低信噪比直扩信号扩频码的盲估计问题,提出了利用小波降噪的方法对直扩信号进行预处理,然后再分别运用特征值分解算法、
八面体中空银纳米笼的高效合成及其表面增强拉曼性能研究,黄凤琴,郭林,以八面体Cu2O为模板,柠檬酸钠作为保护剂,NaBH4和AgNO3反应产生的银纳米颗粒在模板表面沉积形成银包覆氧化亚铜的核壳结构,最后用乙�
PVP高分子包覆的镍纳米材料的形态控制研究,周苇,郭林,本文报道了辅以PVP(聚乙烯吡咯烷酮)修饰水合肼还原二价镍盐制备镍的纳米材料。通过改变镍盐与PVP的摩尔浓度比、回流温度、盐的种