Welcome to Ray's Blog

Stay Hungry Stay Foolish - Steve Jobs

0%

Android 设置一串字符串不同位置上文字的不同样式


概述

效果图:

不同位置的字符串内容显示不同的样式。

实现源码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
/**
* 设置价格格式化
* @param price 价格
* @param rmbSymbolSize 人民币符号的大小
* @param fenSize 分的大小
* @return
*/
public static SpannableString setAuctionPriceStyle(String price, int rmbSymbolSize, int fenSize) {
SpannableString spanText = new SpannableString("¥" + price);
spanText.setSpan(new AbsoluteSizeSpan(rmbSymbolSize, true), 0, 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
int indexLength = spanText.toString().indexOf(".");
if (indexLength > 0) {
spanText.setSpan(new AbsoluteSizeSpan(fenSize, true), indexLength, spanText.toString().length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
return spanText;
}