android textview 两种字体颜色,Android TextView中文字设置不同的颜色

//文字显示不同颜色的工具类

public class

StringColorUtil {

private Context mContext;

private SpannableStringBuilder

spBuilder;

public StringColorUtil(Context context)

{

this.mContext = context;

}

//单个关键字

//设置在wholeStr中第一次出现的keyStr的颜色

//@param wholeStr  全部文字

//@param keyStr  关键字

//@param keyStrColor 关键字的颜色

public

StringColorUtil fillColor(String wholeStr, String keyStr, int

keyStrColor) {

if

(!TextUtils.isEmpty(wholeStr) &&

!TextUtils.isEmpty(keyStr) &&

wholeStr.contains(keyStr)){

int start =

wholeStr.indexOf(keyStr);

int end = start

keyStr.length();

spBuilder = new

SpannableStringBuilder(wholeStr);

CharacterStyle charaStyle =

new

ForegroundColorSpan(mContext.getResources().getColor(keyStrColor));

spBuilder.setSpan(charaStyle,

start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

return this;

} else

{

return null;

}

}

//多个关键字

//设置在wholeStr中第一次出现的keyStr的颜色

//@param wholeStr  全部文字

//@param keyStrMap key:关键字

Value:颜色

public StringColorUtil fillColorByStr(String

wholeStr, HashMap keyStrMap) {

if(!TextUtils.isEmpty(wholeStr)){

spBuilder = new

SpannableStringBuilder(wholeStr);

Set keySet =

keyStrMap.keySet();//将Map中所有的键存入到set集合中

for(String keyStr :

keySet){

if(!TextUtils.isEmpty(keyStr)

&&

wholeStr.contains(keyStr)){

CharacterStyle charaStyle = new

ForegroundColorSpan(mContext.getResources().getColor(keyStrMap.get(keyStr)));

int start

= wholeStr.indexOf(keyStr);

int end =

start keyStr.length();

spBuilder.setSpan(charaStyle,start,end,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

}

}

return this;

}else

{

return null;

}

}

//多个关键字

//设置指定位置的keyStr的颜色

//@param wholeStr  全部文字

//@param keyStringInfos 关键字的一些信息(起始位置,结束位置,颜色值)

public StringColorUtil fillColorByIndex(String wholeStr,

List keyStringInfos) {

if(!TextUtils.isEmpty(wholeStr)){

spBuilder = new

SpannableStringBuilder(wholeStr);

for(KeyStringInfo

keyStringInfo : keyStringInfos){

int start = keyStringInfo.getStart();

int end = keyStringInfo.getEnd();

if(start >=0

&& end <

wholeStr.length()

&& start

<= end){

CharacterStyle charaStyle = new

ForegroundColorSpan(mContext.getResources().getColor(keyStringInfo.getColor()));

spBuilder.setSpan(charaStyle,start,end,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

}

}

return this;

}else

{

return null;

}

}

public SpannableStringBuilder getResult()

{

if

(spBuilder != null) {

return spBuilder;

}

return

null;

}

public static class KeyStringInfo{

//起始位置,结束位置,颜色值

int

start;

int

end;

int

color;

public

KeyStringInfo(int start,int end,int color){

this.start =

start;

this.end = end;

this.color =

color;

}

public int

getStart() {

return start;

}

public

void setStart(int start) {

this.start =

start;

}

public int

getEnd() {

return end;

}

public

void setEnd(int end) {

this.end = end;

}

public int

getColor() {

return color;

}

public

void setColor(int color) {

this.color =

color;

}

}

}

//调用方法

//方法1:

StringColorUtil

stringColorUtil1 = new StringColorUtil(this).fillColor("Hello

Word, 你好!","Hello",R.color.blue_color);

textView.setText(stringColorUtil1.getResult());

//方法2:

HashMap keyString = new

HashMap<>();

keyString.put("Hello",R.color.yellow_color);

keyString.put("o",R.color.red_color);

keyString.put("o",R.color.red_color);

keyString.put("你好",R.color.blue_color);

StringColorUtil

stringColorUtil = new StringColorUtil(this).fillColorByStr("Hello

Word, 你好!",keyString);

textView1.setText(stringColorUtil.getResult());

//方法3:

List keyStringInfos = new

ArrayList<>();

keyStringInfos.add(new

StringColorUtil.KeyStringInfo(4,5,R.color.yellow_color));

keyStringInfos.add(new

StringColorUtil.KeyStringInfo(7,8,R.color.red_color));

keyStringInfos.add(new

StringColorUtil.KeyStringInfo(11,13,R.color.blue_color));

StringColorUtil

stringColorUtil2 = new StringColorUtil(this).fillColorByIndex(str,keyStringInfos);

textView2.setText(stringColorUtil2.getResult());

显示效果图:

a4c26d1e5885305701be709a3d33442f.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值