android中怎么设置没一个string的颜色
发布网友
发布时间:2022-05-25 02:31
我来回答
共2个回答
热心网友
时间:2023-11-25 05:30
设置方法如下:
String str = "美女们我爱你";
String str1 = "美女们";
if (!TextUtils.isEmpty(str)) {
SpannableStringBuilder builder = new SpannableStringBuilder(str);
// ForegroundColorSpan 为文字前景色,BackgroundColorSpan为文字背景色
ForegroundColorSpan redSpan = new ForegroundColorSpan(Color.Red);
ForegroundColorSpan blackSpan = new ForegroundColorSpan(Color.Black);
builder.setSpan(redSpan, 0, str1.length(),
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
builder.setSpan(blackSpan, str1.length(), str.length(),
Spannable.SPAN_INCLUSIVE_INCLUSIVE);
TV.setText(builder);
}
热心网友
时间:2023-11-25 05:31
//第一种方式
Html.fromHtml("<font color='#145A14'>text</font>");
//第二种方式
TextView TV = (TextView)findViewById(R.id.mytextview01);
Spannable wordtoSpan = new SpannableString("I know just how to whisper, And I know just how to cry,I know just where to find the answers");
wordtoSpan.setSpan(new ForegroundColorSpan(Color.BLUE), 15, 30, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
TV.setText(wordtoSpan);