SpannableString s = new SpannableString("MPAndroidChart developed\n by Philipp Jahoda");
s.setSpan(new RelativeSizeSpan(1.7f), 0, 14, 0);
s.setSpan(new StyleSpan(Typeface.NORMAL), 14, s.length() - 15, 0);
s.setSpan(new ForegroundColorSpan(Color.GRAY), 14, s.length() - 15, 0);
s.setSpan(new RelativeSizeSpan(.8f), 14, s.length() - 15, 0);
s.setSpan(new StyleSpan(Typeface.ITALIC), s.length() - 14, s.length(), 0);
s.setSpan(new ForegroundColorSpan(ColorTemplate.getHoloBlue()), s.length() - 14, s.length(), 0);
////////////////////////////////////
//设置字体颜色
textview1 = (TextView) findViewById(R.id.text1);
SpannableStringBuilder spannableStringBuilder1 = new SpannableStringBuilder("Android");
ForegroundColorSpan foregroundColorSpan = new ForegroundColorSpan(Color.BLUE);
spannableStringBuilder1.setSpan(foregroundColorSpan, 0, 3, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
textview1.setText(spannableStringBuilder1);
/////////////////////////////////
//设置字体背景颜色
textview2 = (TextView) findViewById(R.id.text2);
SpannableStringBuilder spannableStringBuilder2 = new SpannableStringBuilder("Android");
BackgroundColorSpan backgroundColorSpan = new BackgroundColorSpan(Color.RED);
spannableStringBuilder2.setSpan(backgroundColorSpan, 0, 3, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
textview2.setText(spannableStringBuilder2);
/////////////////////////
设置字体大小
textview3 = (TextView) findViewById(R.id.text3);
SpannableStringBuilder spannableStringBuilder3 = new SpannableStringBuilder("Android");
AbsoluteSizeSpan absoluteSizeSpan = new AbsoluteSizeSpan(30);
spannableStringBuilder3.setSpan(absoluteSizeSpan, 0, 3, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
textview3.setText(spannableStringBuilder3);
////////////////////////////
设置字体
//设置字体(加粗斜体)
textview4 = (TextView) findViewById(R.id.text4);
SpannableStringBuilder spannableStringBuilder4 = new SpannableStringBuilder("Android");
StyleSpan styleSpan = new StyleSpan(Typeface.BOLD_ITALIC);
spannableStringBuilder4.setSpan(styleSpan, 0, 3, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
textview4.setText(spannableStringBuilder4);
///////////////////////////////////
//设置下划线
textview5 = (TextView) findViewById(R.id.text5);
SpannableStringBuilder spannableStringBuilder5 = new SpannableStringBuilder("Android");
UnderlineSpan underlineSpan = new UnderlineSpan();
spannableStringBuilder5.setSpan(underlineSpan, 0, 3, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
textview5.setText(spannableStringBuilder5);
//////////////////////////
//设置删除线
textview6 = (TextView) findViewById(R.id.text6);
SpannableStringBuilder spannableStringBuilder6 = new SpannableStringBuilder("Android");
StrikethroughSpan strikethroughSpan = new StrikethroughSpan();
spannableStringBuilder6.setSpan(strikethroughSpan, 0, 3, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
textview6.setText(spannableStringBuilder6);
////////////////////////////////
//设置多种样式
textview7 = (TextView) findViewById(R.id.text7);
SpannableStringBuilder spannableStringBuilder7 = new SpannableStringBuilder("Android");
spannableStringBuilder7.setSpan(foregroundColorSpan, 0, 3, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
spannableStringBuilder7.setSpan(backgroundColorSpan, 0, 3, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
spannableStringBuilder7.setSpan(underlineSpan, 0, 3, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
spannableStringBuilder7.setSpan(absoluteSizeSpan, 3, 6, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
spannableStringBuilder7.setSpan(strikethroughSpan, 3, 6, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
textview7.setText(spannableStringBuilder7);
//////////////////////////////
SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder("Android");
spannableStringBuilder.setSpan(
new ClickableSpan() {
@Override
public void onClick(View widget) {
//do something
}
@Override
public void updateDrawState(TextPaint ds) {
//设置一些样式
//ds.setUnderlineText(false);
//ds.setColor(color);
}
}, startIndex, endIndex,
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE
);
/////////////////////
找下标
String s = tv.getText().toString();
int i1 = s.indexOf(":");
类似qq回复,让字体颜色不同
最新推荐文章于 2022-05-31 20:05:49 发布