在开发中我们有的时候会遇到这种需求:如给文字加阴影,一个字符串中部分显示的颜色不同等等来美化文字显示的效果,这个时候我们就会下面这些东西
效果图:
textView1.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG ); // 下划线
textView1.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG); // 删除线
或者
String str = "下划线删除线";
SpannableString span = new SpannableString(str);
span.setSpan(new UnderlineSpan(), 0, 3, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); // 下划线
span.setSpan(new StrikethroughSpan(), 3, 6, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); // 删除线
textView1.setText(span);
String str = "百度电话短信邮件";
SpannableString spanStr = new SpannableString(str);
spanStr.setSpan(new URLSpan("http://www.baidu.com"), 0, 2, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); // 网站
spanStr.setSpan(new URLSpan("tel:15012341234"), 2, 4, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); // 电话
spanStr.setSpan(new URLSpan("sms:15012341234"), 4, 6, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); // 短信,或者smsto
spanStr.setSpan(new URLSpan("mailto:[email protected]"), 6, 8, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); // 邮件
textView2.setText(spanStr);textView2.setMovementMethod(LinkMovementMethod.getInstance()); //添加setMovementMethod方法附加响应
String str = "背景高亮";
SpannableString sp = new SpannableString(str);
sp.setSpan(new BackgroundColorSpan(Color.BLUE), 0, str.length() ,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
textView3.setText(sp);
String str= "文字高亮";
SpannableString sp = new SpannableString(str);
sp.setSpan( new ForegroundColorSpan(Color.RED), 0, str.length(),Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
textView4.setText(sp)
String str = "正常粗体斜体粗斜体";
SpannableString msp = new SpannableString(str);
msp.setSpan(new StyleSpan(android.graphics.Typeface.NORMAL), 0, 2, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); //正常
msp.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), 2, 4, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); //粗体
msp.setSpan(new StyleSpan(android.graphics.Typeface.ITALIC), 4, 6, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); //斜体
msp.setSpan(new StyleSpan(android.graphics.Typeface.BOLD_ITALIC), 6, 9, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); //粗斜体
textView5.setText(msp);
String str = "X1X2";
SpannableString sp=new SpannableString(str);
sp.setSpan(new SuperscriptSpan(), 1, 2, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); //上标
sp.setSpan(new SubscriptSpan(), 3, 4, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); //下标
textView6.setText(sp);
String str = "文字";
SpannableString sp=new SpannableString(str);
sp.setSpan(new ScaleXSpan(2.0f), 0, 2, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); //ScaleXSpan参数表示默认字体宽度的两倍,高度不变
textView7.setText(sp);