/**
* 设置同一个TextView中显示不同的字体颜色和不同的字体大小
* @param context
* @param prescriptionName
* @param totalMedicines
* @return 返回值用于textView.setText(SpannableStringBuilder style)即可显示;
*/
private SpannableStringBuilder handleStyle(Context context, String prescriptionName, String totalMedicines) {
SpannableStringBuilder style = new SpannableStringBuilder(prescriptionName + totalMedicines);
//设置不同的字体颜色,可调用多次
style.setSpan(new ForegroundColorSpan(context.getResources().getColor(R.color.title_color)), 0, prescriptionName.length(), Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
style.setSpan(new ForegroundColorSpan(context.getResources().getColor(R.color.hint_text_color)), prescriptionName.length(), (prescriptionName + totalMedicines).length(), Spanned.SPAN_EXCLUSIVE_INCLUSIVE);
//设置不同的字体大小,可调用多次
style.setSpan(new AbsoluteSizeSpan(Utils.dip2px(context, 16)), 0, prescriptionName.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);
style.setSpan(new AbsoluteSizeSpan(Utils.dip2px(context, 14)), prescriptionName.length(), (prescriptionName + totalMedicines).length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);
return style;
}
转自:http://blog.youkuaiyun.com/yinzhijiezhan/article/details/53198049
本文介绍了一种在Android中使用SpannableStringBuilder来实现同一个TextView内不同字体颜色及大小的方法。通过具体的代码示例,展示了如何为TextView中的不同部分设置独立的颜色和大小。
754

被折叠的 条评论
为什么被折叠?



