1. 程式人生 > >android上實現富文字

android上實現富文字

控制元件上設定富文字可以減少很多不必要的佈局

樣例程式碼:

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);

將btn或者textview處理後效果如下: