2014-10-29 47 views
0

我有一個自定義類,如下所示:如何保持兩個不同的字體大小兩個字符串在一個單一的文本視圖

public class CustomTypefaceSpan extends TypefaceSpan { 
    private final Typeface newType; 

    public CustomTypefaceSpan(String family, Typeface type) { 
     super(family); 
     newType = type; 
    } 

    @Override 
    public void updateDrawState(TextPaint ds) { 
     applyCustomTypeFace(ds, newType); 
    } 

    @Override 
    public void updateMeasureState(TextPaint paint) { 
     applyCustomTypeFace(paint, newType); 
    } 

    private static void applyCustomTypeFace(Paint paint, Typeface tf) { 
     int oldStyle; 
     Typeface old = paint.getTypeface(); 
     if (old == null) { 
      oldStyle = 0; 
     } else { 
      oldStyle = old.getStyle(); 
     } 

     int fake = oldStyle & ~tf.getStyle(); 
     if ((fake & Typeface.BOLD) != 0) { 
      paint.setFakeBoldText(true); 
     } 

     if ((fake & Typeface.ITALIC) != 0) { 
      paint.setTextSkewX(-0.25f); 
     } 

     paint.setTypeface(tf); 
    } 
} 

我申請的範圍如下:

Spannable spannable = new SpannableString(dispStatsPart1()+"\n"+dispStatswords()+"Months"); 
         spannable.setSpan(new CustomTypefaceSpan("sans-serif",Helv_cond_bold), 0, dispStatsPart1().length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); 
         spannable.setSpan(new CustomTypefaceSpan("sans-serif",helv_light), dispStatsPart1().length(), dispStatsPart1().length() +(dispStatswords()+"Months").length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); 
         holder.numberText.setText(spannable); 

我想第一行不同的字體大小和第二行不同的字體大小,如何實現?

到目前爲止,我已經嘗試修改我的代碼:

private static void applyCustomTypeFace(Paint paint, Typeface tf) { 
     int oldStyle; 
     Typeface old = paint.getTypeface(); 
     if (old == null) { 
      oldStyle = 0; 
     } else { 
      oldStyle = old.getStyle(); 
     } 

     int fake = oldStyle & ~tf.getStyle(); 
     if ((fake & Typeface.BOLD) != 0) { 
      paint.setFakeBoldText(true); 
     } 

     if ((fake & Typeface.ITALIC) != 0) { 
      paint.setTextSkewX(-0.25f); 
     } 
     if(flag == 0){ 
      paint.setTextSize(50); 
     }else{ 
      paint.setTextSize(50); 
     } 

     paint.setTypeface(tf); 
    } 

我通過標誌來構造:

public CustomTypefaceSpan(String family, Typeface type, int flg) { 
     super(family); 
     newType = type; 
     flag = flg; 
    } 

但上面似乎並沒有工作,字體大小增加或減少,但它發生在兩個字符串中。

+0

請參閱本http://stackoverflow.com/questions/16335178/different-size-of-strings-in-the-same-textview – 2014-10-29 10:15:44

+0

雖然你的答案是有益的,但我使用的是自定義類和我想知道在這方面的實施。 – User3 2014-10-29 10:24:43

回答

0
yourTextView.setText(Html.fromHtml("xxx"))  
1

如果您想知道如何在同一個TextView中設置多個不同的尺寸,但使用絕對大小,而不是相對的,你可以做到這一點使用AbsoluteSpan。

dimens.xml

<dimen name="text_size_1">18sp</dimen> 
<dimen name="text_size_2">12sp</dimen> 

剛拿到尺寸所需的文本大小的像素

int textSize1 = getResources().getDimensionPixelSize(R.dimen.text_size_1); 
int textSize2 = getResources().getDimensionPixelSize(R.dimen.text_size_2); 

,然後基於文本

String text1 = "text1 font size is 18sp"; 
String text2 = "text2 font size is 12sp "; 

SpannableString span1 = new SpannableString(text1); 
span1.setSpan(new AbsoluteSizeSpan(textSize1), 0, text1.length(), SPAN_INCLUSIVE_INCLUSIVE); 

SpannableString span2 = new SpannableString(text2); 
span2.setSpan(new AbsoluteSizeSpan(textSize2), 0, text2.length(), SPAN_INCLUSIVE_INCLUSIVE); 

// let's put both spans together with a separator and all 
CharSequence finalText = TextUtils.concat(span1, "\n", span2); 

這裏創建一個新的AbsoluteSpan是輸出:

Two different font sizes for two strings in one single text view

相關問題