【问题标题】:Roboto light and Roboto bold in a TextViewTextView 中的 Roboto 灯和 Roboto 粗体
【发布时间】:2014-03-24 09:46:54
【问题描述】:
我可以像这样在 Android 2.3 上的同一个 TextView 中应用 Roboto light 和 Roboto bold 吗?
**user** has been publish a beez
**user** 是 Roboto 粗体,has been publish a beez 是 Roboto 浅色
【问题讨论】:
标签:
java
android
textview
spannablestring
【解决方案1】:
是的,你可以这样做..
String firstWord = "user";
String secondWord = "has been publish a beez";
// Create a new spannable with the two strings
Spannable spannable = new SpannableString(firstWord+secondWord);
// Set the custom typeface to span over a section of the spannable object
spannable.setSpan( new CustomTypefaceSpan("sans-serif",CUSTOM_TYPEFACE), 0, firstWord.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
spannable.setSpan( new CustomTypefaceSpan("sans-serif-light",SECOND_CUSTOM_TYPEFACE), firstWord.length(), firstWord.length() + secondWord.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
// Set the text of a textView with the spannable object
textView.setText( spannable );
您可以像这样从 Android 4.1+ 原生使用 Roboto:
android:fontFamily="sans-serif" // roboto regular
android:fontFamily="sans-serif-light" // roboto light
android:fontFamily="sans-serif-condensed"