Android實現RichText(富文本)不同Text樣式

來源:互聯網
上載者:User

標籤:

同一個TextView裡面顯示不同風格的文字。類似這種。 主要的基本工具類有android.text.Spanned; android.text.SpannableString; android.text.SpannableStringBuilder;使用這些類來代替常規String。SpannableString和SpannableStringBuilder可以用來設定不同的Span,這些Span便是用於實現Rich Text,比如粗體,斜體,前景色彩,背景色,字型大小,字型風格等。  使用:當要顯示Rich Text資訊的時候,可以使用建立一個SpannableString或SpannableStringBuilder,它們的區別在於SpannableString像一個String一樣,構造對象的時候傳入一個String,之後再無法更改String的內容,也無法拼接多個SpannableString;而SpannableStringBuilder則更像是StringBuilder,它可以通過其append()方法來拼接多個String。接著,可以直接把SpannableString和SpannableStringBuilder通過TextView.setText()設定給TextView。
  1. finalTextView textWithString =(TextView) findViewById(R.id.text_view_font_1);String w ="The quick fox jumps over the lazy dog";int start = w.indexOf(‘q‘);int end = w.indexOf(‘k‘)+1;Spannable word =newSpannableString(w);word.setSpan(newAbsoluteSizeSpan(22), start, end,Spannable.SPAN_INCLUSIVE_INCLUSIVE);
setSpan參數:AbsoluteSizeSpan(int size) ---- 設定字型大小,參數是絕對數值,相當於Word中的字型大小; RelativeSizeSpan(float proportion) ---- 設定字型大小,參數是相對於預設字型大小的倍數,比如預設字型大小是x, 那麼設定後的字型大小就是x*proportion,這個用起來比較靈活,proportion>1就是放大(zoom in), proportion<1就是縮小(zoom out); ScaleXSpan(float proportion) ---- 縮放字型,與上面的類似,預設為1,設定後就是原來的乘以proportion,大於1時放大(zoon in),小於時縮小(zoom out) BackgroundColorSpan(int color) ----背景著色,參數是顏色數值,可以直接使用android.graphics.Color裡面定義的常量,或是用Color.rgb(int, int, int); ForegroundColorSpan(int color) ----前景著色,也就是字的著色,參數與背景著色一致 TypefaceSpan(String family) ----字型,參數是字型的名字比如“sans", "sans-serif"等; StyleSpan(Typeface style) -----字型風格,比如粗體,斜體,參數是android.graphics.Typeface裡面定義的常量,如Typeface.BOLD,Typeface.ITALIC等等。 StrikethroughSpan----如果設定了此風格,會有一條線從中間穿過所有的字,就像被劃掉一樣;這裡有個如果設定自訂的字型:要記得擷取getStyle。
  1. privateTypeface mRegularTypeFace;mRegularTypeFace =Typeface.createFromAsset(getAssets(),"fonts/AvenirNext-Regular.ttf");word.setSpan(newStyleSpan(mBoldTypeFace.getStyle()), start, end,Spannable.SPAN_INCLUSIVE_INCLUSIVE);mMyLevel.setText(word);
參數what:設定的Style span,start和end則是標識String中Span的起始位置,而 flags是用於控制行為的,通常設定為0或Spanned中定義的常量,常用的有:Spanned.SPAN_EXCLUSIVE_EXCLUSIVE --- 不包含兩端start和end所在的端點;Spanned.SPAN_EXCLUSIVE_INCLUSIVE --- 不包含端start,但包含end所在的端點;Spanned.SPAN_INCLUSIVE_EXCLUSIVE --- 包含兩端start,但不包含end所在的端點;Spanned.SPAN_INCLUSIVE_INCLUSIVE--- 包含兩端start和end所在的端點;  Linkify :另外,也可以對通過TextView.setAutoLink(int)設定其Linkify屬性,其用處在於,TextView會自動檢查其內容,會識別出phone number, web address or email address,並標識為超連結,可點擊,點擊後便跳轉到相應的應用。 參考:http://blog.csdn.net/hitlion2008/article/details/6856780  

 

Android實現RichText(富文本)不同Text樣式

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.