您的位置:首页 > 移动开发 > Android开发

Android文本样式(一)

2015-11-13 11:05 531 查看
在android中,有时候需要对文本进行各种特别的设置,比如颜色、大小、首行缩进,或者是在一段文本中加入图片,甚至是书写一些特殊的公式。如果通过布局文件使用多个控件来实现,一方面会使的使用起来特别的复杂,增加了布局文件维护的难度,另一方面,如果加入了太多的控件,在页面加载时也要耗费更多的资源。如果在HTML中,则可以使用各种标签来实现这些特殊效果,而在android中有类似的机制,只不过不是使用标签来实现,而是使用Spannable对象来实现。


一、关于Spannable的构建:



可以看出,Spannable继承自Spanned接口,而实际上,Spanned继承自CharSequence接口:



在TextView的setText(CharSequence text)方法中,要求的参数正好是一个CharSequence对象,因此,我们可以通过Spannable对象来直接使用setText来完成文本的设置。在使用中通常使用Spannable spn = new SpannableString("字符串");或者通过SpannableStringBuilder对象来进行构建。


二、给Spannable对象设置样式:

在构建除了Spannable对象以后,就可以使用spannable.setSpan(Obj what, int start, int end, int flags)方法来进行样式的设置了,其中参数what是具体样式的实现对象,start则是该样式开始的位置,end对应的是样式结束的位置,参数flags,定义在Spannable中的常量,常用的有:

Spanned.SPAN_EXCLUSIVE_EXCLUSIVE --- 不包含两端start和end所在的端点 (a,b)

Spanned.SPAN_EXCLUSIVE_INCLUSIVE --- 不包含端start,但包含end所在的端点 (a,b]

Spanned.SPAN_INCLUSIVE_EXCLUSIVE --- 包含两端start,但不包含end所在的端点 [a,b)

Spanned.SPAN_INCLUSIVE_INCLUSIVE--- 包含两端start和end所在的端点 [a,b]

但实际测试这其中似乎并未有差别,而在start和end相同的情况下,则只对start所在字符的当前行起作用。


三、样式分析:

用来构建样式的具体的类都在android.text.style包下,下面逐一进行分析。
1.AbsoluteSizeSpan

顾名思义,AbsoluteSizeSpan是指绝对尺寸,通过指定绝对尺寸来改变文本的字体大小。该类有三个构造函数:

AbsoluteSizeSpan(int size)、AbsoluteSizeSpan(int size, boolean dip)、AbsoluteSizeSpan(Parcel src)。

AbsoluteSizeSpan(int size):参数size, 以size的指定的像素值来设定文本大小。

AbsoluteSizeSpan(int size, boolean dip):参数size,以size的指定像素值来设定文本大小,如果参数dip为true则以size指定的dip为值来设定文本大小。

AbsoluteSizeSpan(Parcel src):参数src,包含有size和dip值的包装类。在该构造中

public AbsoluteSizeSpan(Parcel src) {

mSize = src.readInt();

mDip = src.readInt() != 0;

}

使用范例:

Parcel p = Parcel.obtain();

p.writeInt(29);//字体大小

p.writeInt(8);//是否是dip单位

p.setDataPosition(0);

AbsoluteSizeSpan ass = new AbsoluteSizeSpan(p);

效果:



2.AlignmentSpan.Standard

AlignmentSpan.Standard,标准文本对齐样式,该类有两个构造函数,AlignmentSpan.Standard(Layout.Alignment align)和AlignmentSpan.Standard(Parcel src)。AlignmentSpan.Standard(Layout.Alignment align):参数align,Layout.Alignment类型的枚举值。包括居中、正常和相反三种情况。

AlignmentSpan.Standard(Parcel src):参数src,包含有标准字符串的Parcel类,其值应为"ALIGN_CENTER"、"ALIGN_NORMAL"或"ALIGN_OPPOSITE"中的之一,对应Layout.Alignment枚举中的三个类型。使用示例:

Parcel p = Parcel.obtain();

p.writeString(\"ALIGN_CENTER\");

p.setDataPosition(0);

AlignmentSpan.Standard standard = new AlignmentSpan.Standard(p);

效果:



3.BackgroundColorSpan

BackgroundColorSpan,背景色样式,显然可以用来设定文本的背景色。该类有两个构造函数,BackgroundColorSpan(int color)和BackgroundColorSpan(Parcel src)。

BackgroundColorSpan(int color):参数color,颜色值。

BackgroundColorSpan(Parcel src):参数src,包含颜色值信息的包装类,使用方法如下:

Parcel p = Parcel.obtain();

p.writeInt(Color.GREEN);

p.setDataPosition(0);

BackgroundColorSpan bcs = new BackgroundColorSpan(p);

效果:



4.BulletSpan

BulletSpan,着重样式,类似于HTML中的<li>标签的圆点效果。该类有4个构造函数BulletSpan()、BulletSpan(int gapWidth)、BulletSpan(int gapWidth,int color)、BulletSpan(Parcel src)。

BulletSpan():仅提供一个与文本颜色一致的符号。

BulletSpan(int gapWidth): 提供一个与文本颜色一致的符号,并指定符号与后面文字之间的空白长度。

BulletSpan(int gapWidth,int color):提供一个指定颜色的符号,并指定符号与后面文字之间的宽度。

BulletSpan(Parcel src):参数src,包含宽度、颜色信息的包装类,在以此构造时,构造函数的调用如下:

mGapWidth = src.readInt();

mWantColor = src.readInt() != 0;\nmColor = src.readInt();

如果使用Parcel作为参数时,使用方式为:

Parcel p = Parcel.obtain();

p.writeInt(20);//设置gapWidth

p.writeInt(1);//设置是否使用颜色

p.writeInt(Color.YELLOW);//设置颜色

p.setDataPosition(0);

BulletSpan bs3 = new BulletSpan(p);

效果:



5.DrawableMarginSpan

DrawableMarginSpan,图片+Margin样式,该类有两个构造函数:DrawableMarginSpan(Drawable b)、DrawableMarginSpan(Drawable b,int pad)。

DrawableMarginSpan(Drawable b):参数b,用于显示的图片。

DrawableMarginSpan(Drawable b,int pad):参数b,用于显示的图片,参数pad,图片和文字的距离。

效果:



6.ForegroundColorSpan

ForegroundColorSpan,字体颜色样式,用于改变字体颜色。该类有两个构造函数:ForegroundColorSpan(int color)、ForegroundColorSpan(Parcel src)。

ForegroundColorSpan(int color):参数color,字体颜色。

ForegroundColorSpan(Parcel src):参数src,包含字体颜色信息的包装类,使用如下:

Parcel p = Parcel.obtain();

p.writeInt(Color.YELLOW);

p.setDataPosition(0);

ForegroundColorSpan fcs = new ForegroundColorSpan(p);

效果:



7.IconMarginSpan

IconMarginSpan,图标+Margin样式,该类与DrawableMarginSpan使用上很相似。本类有两个构造函数:

IconMarginSpan(Bitmap b):参数b,用于显示图像的bitmap。

IconMarginSpan(Bitmap b,int pad):参数b,用于显示图像的bitmap,参数pad,Bitmap和文本之间的间距。

效果:



8.ImageSpan

ImageSpan,图片样式,主要用于在文本中插入图片。本类构造函数较多,但主要是针对Bitmap和Drawable的,也可以通过资源Id直接加载图片。如下:

ImageSpan(Bitmap b):.参数b,用于显示的Bitmap。该方法已过时,改用Use ImageSpan(Context, Bitmap)代替。

ImageSpan(Bitmap b, int verticalAlignment):参数b,用于显示的Bitmap,参数verticalAlignment,对齐方式,对应ImageSpan中的常量值。该方法已过时,改用ImageSpan(Context, Bitmap, int)代替。

ImageSpan(Context context, Bitmap b):参数context,传入的上下文,参数b,用于显示的Bitmap。

ImageSpan(Context context, Bitmap b, int verticalAlignment):参数context,传入的上下文,参数b,用于显示的Bitmap,参数verticalAlignment,对齐方式。

ImageSpan(Drawable d):参数d,用于显示的Drawable,此Drawable须设置大小。

ImageSpan(Drawable d, int verticalAlignment):参数d,用于显示的Drawable,参数verticalAlignment,对齐方式。

ImageSpan(Drawable d, String source):参数d,用于显示的Drawable,参数source,资源字符串。

ImageSpan(Drawable d, String source, int verticalAlignment):参数d,用于显示的Drawable,参数source,资源字符串,参数verticalAlignment,对齐方式。

ImageSpan(Context context, Uri uri):参数context,传入的上下文,参数uri,图片的uri。

ImageSpan(Context context, Uri uri, int verticalAlignment):参数context,传入的上下文,参数uri,图片的uri,参数verticalAlignment,对齐方式。

ImageSpan(Context context, int resourceId):参数context,传入的上下文,参数resourceId,图片的资源id。

ImageSpan(Context context, int resourceId, int verticalAlignment)参数context,传入的上下文,参数resourceId,图片的资源id,参数verticalAlignment,对齐方式。

效果:



9.LeadingMarginSpan

LeadingMarginSpan.Standard,文本缩进的样式。有3个构造函数,分别为:

Standard(int arg0):参数arg0,缩进的像素。

Standard(int arg0, int arg1):参数arg0,首行缩进的像素,arg1,剩余行缩进的像素。

Standard(Parcel p): 参数p,包含缩进信息的包装类。在构造时,

public Standard(Parcel src) {

mFirst = src.readInt();\n"+

mRest = src.readInt();\n"+

}

使用方式:

Parcel p = Parcel.obtain();

p.writeInt(20);

p.writeInt(30);

p.setDataPosition(0);

Standard lms = new Standard(p);

效果:



转载地址:http://blog.csdn.net/lixin84915/article/details/8110667
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: