Android中对同一个TextView设置不同字体样式

这个只是做一个笔记,详情见代码:

代码中的注释也有说明。

TextView textView = (TextView) findViewById(R.id.textView1);
		SpannableStringBuilder sb = new SpannableStringBuilder("abcdefghijklmnopqrst."); // 包装字体内容
		ForegroundColorSpan fcs = new ForegroundColorSpan(getResources().getColor(R.color.yellow_orange)); // 设置字体颜色
		StyleSpan bss = new StyleSpan(android.graphics.Typeface.BOLD); // 设置字体样式
		AbsoluteSizeSpan ass = new AbsoluteSizeSpan(140);  // 设置字体大小
		
		sb.setSpan(fcs, 0, 10, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
		sb.setSpan(bss, 0, 20, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
		sb.setSpan(ass, 0, 5, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
		textView.setText(sb);
运行结果如下:

Android中对同一个TextView设置不同字体样式_第1张图片

转载于:https://www.cnblogs.com/fengju/p/6336141.html

你可能感兴趣的:(Android中对同一个TextView设置不同字体样式)