這可能工作 - 創建自己的自定義的TextView,然後在它的一個組成部分使用StyleSpan:
public class CustomTextView extends TextView {
public CustomTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public void setTypeface(Typeface tf, int style) {
if (style == 1){
//replace "HelveticaBOLD.otf" with the name of your bold font
tf = Typeface.createFromAsset(getContext().getApplicationContext().getAssets(), "HelveticaBOLD.otf");
}else{
//replace "HelveticaNORMAL.otf" with the name of your normal font
tf = Typeface.createFromAsset(getContext().getApplicationContext().getAssets(), "HelveticaNORMAL.otf");
}
super.setTypeface(tf, 0);
}
}
而且那麼你可以做這樣的事情:
int index1 = 0; //wherever bold should begin
int index2 = 5; //wherever bold should end
Spannable span = new SpannableString("some string");
span.setSpan(new StyleSpan(Typeface.BOLD),index1, index2,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
((CustomTextView)findViewById(R.id.yourTextView)).setText(span);
,你爲什麼不能使用具有不同fontstyles兩種不同的TextView。 – 2012-03-08 14:17:49
@SenthilMg你可以在問題的參考鏈接上看到原因。它不會格式化文本,因爲它是必需的 – rizzz86 2012-03-08 14:33:49
,我猜想通過使用正則表達式將完整字符串拆分爲兩個:並設置在不同的文本視圖爲例如:String text = AAAA:BBBBB,String [] splitText = text.split 「:」); tv1.setText(splitText [0]); tv2.setText(splitText [1]); – 2012-03-08 14:39:15