本文转载自:http://www.2cto.com/kf/201512/455755.html
49.Android 各种Span
Android 各种Span 前言 SpannableStringBuilder URLSpan UnderlineSpan TypefaceSpan TextAppearanceSpan TabStopSpanStandard SuperscriptSpan SubscriptSpan StrikethroughSpan ScaleXSpan StyleSpan RelativeSizeSpan QuoteSpan MaskFilterSpan LeadingMarginSpanStandard ImageSpan IconMarginSpan ForegroundColorSpan DrawableMarginSpan BulletSpan BackgroundColorSpan AlignmentSpanStandard AbsoluteSizeSpan ClickableSpan 源代码
前言
在android.text.style
包下,有一些Span类,可以提供我们完成一些在TextView中的特殊内容。(比如:部分内容颜色、字体、大小不同等等,更有部分字体可点击。)
还有一个SpannableStringBuilder,可以帮助我们设置Span。
底下也有全部的源代码。
SpannableStringBuilder
SpannableStringBuilder
可以方便我们更好的设置上对应的Span。
设置Span
SpannableStringBuilder.setSpan(Object what, int start, int end, int flags)
这里的Flag表示:start和end是开区间还是闭区间。
Flag:
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE —— (a,b)
Spanned.SPAN_EXCLUSIVE_INCLUSIVE —— (a,b]
Spanned.SPAN_INCLUSIVE_EXCLUSIVE —— [a,b)
Spanned.SPAN_INCLUSIVE_INCLUSIVE —— [a,b]
URLSpan
功能:点击文字,可以打开一个URL。
URLSpan(String url)
1
2
3
4
5
6
7
|
<code
class
=
"language-java hljs "
>SpannableStringBuilder ssb =
new
SpannableStringBuilder(content);
ssb.setSpan(
new
URLSpan(
"https://github.com/CaMnter"
), start, sub.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
contentTV.setText(ssb);
// 在单击链接时凡是有要执行的动作,都必须设置MovementMethod对象
contentTV.setMovementMethod(LinkMovementMethod.getInstance());
// 设置点击后的颜色,这里涉及到ClickableSpan的点击背景
contentTV.setHighlightColor(
0xff8FABCC
);</code>
|
![](http://www.2cto.com/uploadfile/Collfiles/20151230/20151230092053154.png)
UnderlineSpan
功能:设置文字下划线。
UnderlineSpan()
1
2
3
|
<code
class
=
"language-java hljs "
>SpannableStringBuilder ssb =
new
SpannableStringBuilder(content);
ssb.setSpan(
new
UnderlineSpan(), start, start + sub.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
contentTV.setText(ssb);</code>
|
UnderlineSpan
功能:设置文字下划线。
UnderlineSpan()
1
2
3
|
<code
class
=
"language-java hljs "
>SpannableStringBuilder ssb =
new
SpannableStringBuilder(content);
ssb.setSpan(
new
UnderlineSpan(), start, start + sub.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
contentTV.setText(ssb);</code>
|
![](http://www.2cto.com/uploadfile/Collfiles/20151230/20151230092053155.png)
TypefaceSpan
功能:设置文字字体。
TypefaceSpan(String family)
构造方法源码中的注释提示了三种系统字体:
monospace
serif
sans-serif
1
2
3
4
5
6
7
|
<code
class
=
"language-java hljs "
>
/**
* @param family The font family for this typeface. Examples include
* "monospace", "serif", and "sans-serif".
*/
public
TypefaceSpan(String family) {
mFamily = family;
}</code>
|
1
2
3
|
<code
class
=
"language-java hljs "
>SpannableStringBuilder ssb =
new
SpannableStringBuilder(content);
ssb.setSpan(
new
TypefaceSpan(
"serif"
), start, start + sub.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
contentTV.setText(ssb);</code>
|
![](http://www.2cto.com/uploadfile/Collfiles/20151230/20151230092053156.png)
TextAppearanceSpan
功能:设置文字字体、文字样式(粗体、斜体、等等)、文字颜色状态、文字下划线颜色状态等等。
TextAppearanceSpan的三个构造方法
TextAppearanceSpan(Context context, int appearance)
TextAppearanceSpan(Context context, int appearance, int colorList)
TextAppearanceSpan(String family, int style, int size,ColorStateList color, ColorStateList linkColor)
family:
monospace
serif
sans-serif
style:
Typeface.NORMAL
Typeface.BOLD
Typeface.ITALIC
Typeface.BOLD_ITALIC
size:表示字体大小(单位px)
1
2
3
4
5
6
7
8
9
10
11
12
13
|
<code
class
=
"language-java hljs "
>SpannableStringBuilder ssb =
new
SpannableStringBuilder(content);
ColorStateList colorStateList =
null
;
if
(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
colorStateList =
this
.activity.getColorStateList(R.color.selector_apperarance_span);
}
else
{
try
{
colorStateList = ColorStateList.createFromXml(
this
.activity.getResources(),
this
.activity.getResources().getXml(R.color.selector_apperarance_span));
}
catch
(XmlPullParserException | IOException e) {
e.printStackTrace();
}
}
ssb.setSpan(
new
TextAppearanceSpan(
"serif"
, Typeface.BOLD_ITALIC,
this
.activity.getResources().getDimensionPixelSize(R.dimen.text_appearance_span), colorStateList, colorStateList), start, start + sub.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
contentTV.setText(ssb);</code>
|
![](http://www.2cto.com/uploadfile/Collfiles/20151230/20151230092053157.png)
TabStopSpan.Standard
功能:每行的MarginLeft的偏移量(跟 \t 和 \n 有关系)。
TabStopSpan.Standard(int where)
1
2
3
4
5
6
7
8
9
10
11
12
13
|
<code
class
=
"language-java hljs "
>SpannableStringBuilder ssb =
new
SpannableStringBuilder(content);
String[] subs = content.split(
" "
);
ssb =
new
SpannableStringBuilder();
/**
* TabStopSpan. Standard related to \t and \n
* TabStopSpan.Standard 跟 \t 和 \n 有关系
*/
for
(String sub1 : subs) {
ssb.append(
"\t"
).append(sub1).append(
" "
);
ssb.append(
"\n"
);
}
ssb.setSpan(
new
TabStopSpan.Standard(
126
),
0
, ssb.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
contentTV.setText(ssb);</code>
|
vcFNwYW4uU3RhbmRhcmQ=" src="http://www.2cto.com/uploadfile/Collfiles/20151230/20151230092053158.png" title="\" />
SuperscriptSpan
功能:文字设置为上标,数学公式中用到。
SuperscriptSpan(Parcel src)
1
2
3
4
5
6
7
8
|
<code
class
=
"language-java hljs "
>SpannableStringBuilder ssb =
new
SpannableStringBuilder(content);
ssb.replace(start, start + sub.length(),
"Save6"
);
Parcel parcel = Parcel.obtain();
parcel.writeInt(
6
);
int
sixPosition = ssb.toString().indexOf(
"6"
);
ssb.setSpan(
new
SuperscriptSpan(parcel), sixPosition, sixPosition +
1
, Spanned.SPAN_INCLUSIVE_INCLUSIVE);
parcel.recycle();
contentTV.setText(ssb);</code>
|
![](http://www.2cto.com/uploadfile/Collfiles/20151230/20151230092053159.png)
SubscriptSpan
功能:文字设置为下标,化学式中用到。
SubscriptSpan(Parcel src)
1
2
3
4
5
6
7
8
|
<code
class
=
"language-java hljs "
>SpannableStringBuilder ssb =
new
SpannableStringBuilder(content);
ssb.replace(start, start + sub.length(),
"Save6"
);
Parcel parcel = Parcel.obtain();
parcel.writeInt(
6
);
int
sixPosition = ssb.toString().indexOf(
"6"
);
ssb.setSpan(
new
SubscriptSpan(parcel), sixPosition, sixPosition +
1
, Spanned.SPAN_INCLUSIVE_INCLUSIVE);
parcel.recycle();
contentTV.setText(ssb);</code>
|
![](http://www.2cto.com/uploadfile/Collfiles/20151230/20151230092053160.png)
StrikethroughSpan
功能:文字设置删除线。
StrikethroughSpan()
1
2
3
|
<code
class
=
"language-java hljs "
>SpannableStringBuilder ssb =
new
SpannableStringBuilder(content);
ssb.setSpan(
new
StrikethroughSpan(), start, start + sub.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
contentTV.setText(ssb);</code>
|
![](http://www.2cto.com/uploadfile/Collfiles/20151230/20151230092053161.png)
ScaleXSpan
功能:文字横向缩放。
ScaleXSpan(float proportion)
proportion:缩放比例
1
2
3
|
<code
class
=
"language-java hljs "
>SpannableStringBuilder ssb =
new
SpannableStringBuilder(content);
ssb.setSpan(
new
ScaleXSpan(
2
.0f), start, start + sub.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
contentTV.setText(ssb);</code>
|
![](http://www.2cto.com/uploadfile/Collfiles/20151230/20151230092053162.png)
StyleSpan
功能:文字设置样式(正常、粗体、斜体、粗斜体)。
StyleSpan(int style)
style:
Typeface.NORMAL
Typeface.BOLD
Typeface.ITALIC
Typeface.BOLD_ITALIC
1
2
3
|
<code
class
=
"language-java hljs "
>SpannableStringBuilder ssb =
new
SpannableStringBuilder(content);
ssb.setSpan(
new
StyleSpan(Typeface.BOLD_ITALIC), start, start + sub.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
contentTV.setText(ssb);</code>
|
![](http://www.2cto.com/uploadfile/Collfiles/20151230/20151230092053163.png)
RelativeSizeSpan
功能:设置文字相对大小,指相对于文本设定的大小的相对比例。
RelativeSizeSpan(float proportion)
proportion:大小比例。
1
2
3
|
<code
class
=
"language-java hljs "
>SpannableStringBuilder ssb =
new
SpannableStringBuilder(content);
ssb.setSpan(
new
RelativeSizeSpan(
6
.0f), start, start + sub.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
contentTV.setText(ssb);</code>
|
![](http://www.2cto.com/uploadfile/Collfiles/20151230/20151230092053164.png)
QuoteSpan![](http://www.2cto.com/uploadfile/Collfiles/20151230/20151230092053165.png)
功能:设置文字左侧显示引用样式(一条竖线)。
QuoteSpan(@ColorInt int color)
color:竖线的颜色。
1
2
3
|
<code
class
=
"language-java hljs "
>SpannableStringBuilder ssb =
new
SpannableStringBuilder(content);
ssb.setSpan(
new
QuoteSpan(
0xff000000
), start, start + sub.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
contentTV.setText(ssb);</code>
|