TextView是安卓中最常见的控件之一了。这一篇基础文章就来梳理下我们最熟悉的TextView。
本文主要关注一些以作者本身来说可能比较模糊的点。常见的烂熟悉于心的点不在此赘述
1.CharSequence与String的区别:
String大家都很熟悉,此处不多说。先看下CharSequence的源码,介绍如下:
This interface does not refine the general contracts of the {@link * java.lang.Object#equals(java.lang.Object) equals} and {@link * java.lang.Object#hashCode() hashCode} methods. The result of comparing two * objects that implement <tt>CharSequence</tt> is therefore, in general, * undefined. Each object may be implemented by a different class, and there * is no guarantee that each class will be capable of testing its instances * for equality with those of the other. It is therefore inappropriate to use * arbitrary <tt>CharSequence</tt> instances as elements in a set or as keys in * a map.
大致意思就是:这是一个接口,没有细化处理equals和hashcode两个方法,所以不能用这两个方法来比较,也不能做Map或Set集合中的key值。
这是一个点。另一个直译过来。CharSequence的意思就是“字符序列”,一组char序列。所以它具有charAt(int index)方法,返回值为char。我们通常使用的时候toString就行了。
2.xml属性
lineSpacingExtra="dimen" 行间距
letterSpacing="0.5" 字间距,设置值为float类型。实际间距=value*字宽,设置0.5则间距为半个字宽
singleline="Boolean" 是否单行
lines="Integer" 行数
ems="1" 字数限制。一行允许的字数。当ems设置时,maxems与minems不起作用