TextView效果实例

106 阅读2分钟

​ ​一起养成写作习惯!这是我参与「掘金日新计划 · 4 月更文挑战」的第21天,点击查看活动详情

 1 TextView的Html效果

代码如下:
        @Override
        public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.main);
                TextView textView = (TextView) findViewById(R.id.cmd100);
                textView.setText(Html.fromHtml("Hello <b>CMD100</b>,<font color="red">  hello GM2.0!!</font>   <font color="#006666"><b>CMD100 GM2.0</b> 即将上线!大家加油,都把自己该准备的准备好咯。</font>"));
        }

2 TextView实现下划线效果

方法1、

tv.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG);//下划线

方法2、

tv.setText(Html.fromHtml("使用html实现下划线样式"));

3 Spanned 实现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:

SpannableString word = new SpannableString("The quick fox jumps over the lazy dog"); 

SpannableStringBuilder multiWord = new SpannableStringBuilder(); 

multiWord.append("The Quick Fox"); 

multiWord.append("jumps over"); 

multiWord.append("the lazy dog");

创建完Spannable对象后,就可以为它们设置Span来实现想要的Rich Text了,常见的Span有:

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.Typeface里面定义的常量,如Typeface.BOLD,Typeface.ITALIC等等。

StrikethroughSpan----如果设置了此风格,会有一条线从中间穿过所有的字,就像被划掉一样

对于这些Sytle span在使用的时候通常只传上面所说明的构造参数即可,不需要设置其他的属性,如果需要的话,也可以对它们设置其他的属性。

SpannableString和SpannableStringBuilder都有一个设置上述Span的方法:

 /** 
     * Set the style span to Spannable, such as SpannableString or SpannableStringBuilder 
     * @param what --- the style span, such as StyleSpan 
     * @param start --- the starting index of characters to which the style span to apply 
     * @param end --- the ending index of characters to which the style span to apply 
     * @param flags --- the flag specified to control 
     */  
    setSpan(Object what, int start, int end, int flags); 

其中参数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所在的端点

这里理解起来就好像数学中定义区间,开区间还是闭区间一样的。这里要重点说明下关于参数0,有很多时候,如果设置了上述的参数,那么Span会从start应用到Text结尾,而不是在start和end二者之间,这个时候就需要使用Flag 0。

和前景色设置案例:

textview=(TextView)findViewById(R.id.textview);      
SpannableStringBuilder style=new SpannableStringBuilder(strs);      
style.setSpan(new BackgroundColorSpan(Color.RED),start,end,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);      
style.setSpan(new ForegroundColorSpan(Color.RED),7,9,Spannable.SPAN_EXCLUSIVE_INCLUSIVE);      
textview.setText(style); 

\