android常用字体代码,Android TextView设置字体风格多种组合
在開發(fā)應(yīng)用過程中經(jīng)常會遇到顯示一些不同的字體風(fēng)格的信息猶如默認(rèn)的LockScreen上面的時間和充電信息。對于類似的情況,可能第一反應(yīng)就是用不同的多個TextView來實現(xiàn),對于每個TextView設(shè)置不同的字體風(fēng)格以滿足需求。
這里推薦的做法是使用Android.text.*;和android.text.style.*;下面的組件來實現(xiàn)RichText:也即在同一個TextView中設(shè)置不同的字體風(fēng)格。對于某些應(yīng)用,比如文本編輯,記事本,彩信,短信等地方,還必須使用這些組件才能達(dá)到想到的顯示效果。
主要的基本工具類有android.text.Spanned; android.text.SpannableString; android.text.SpannableStringBuilder;使用這些類來代替常規(guī)String。SpannableString和SpannableStringBuilder可以用來設(shè)置不同的Span,這些Span便是用于實現(xiàn)Rich Text,比如粗體,斜體,前景色,背景色,字體大小,字體風(fēng)格等等,android.text.style.*中定義了很多的Span類型可供使用。
這是相關(guān)的API的Class General Hierarchy:
因為Spannable等最終都實現(xiàn)了CharSequence接口,所以可以直接把SpannableString和SpannableStringBuilder通過TextView.setText()設(shè)置給TextView。
使用方法當(dāng)要顯示Rich Text信息的時候,可以使用創(chuàng)建一個SpannableString或SpannableStringBuilder,它們的區(qū)別在于SpannableString像一個String一樣,構(gòu)造對象的時候傳入一個String,之后再無法更改String的內(nèi)容,也無法拼接多個SpannableString;而SpannableStringBuilder則更像是StringBuilder,它可以通過其append()方法來拼接多個String:
SpannableString word = new SpannableString("The quick fox jumps over the lazy dog"); SpannableStringBuilder multiWord = new SpannableStringBuilder(); multiWord.append("The Quick Fox"); multiWord.append("jumps over"); multiWord.append("the lazy dog");
創(chuàng)建完Spannable對象后,就可以為它們設(shè)置Span來實現(xiàn)想要的Rich Text了,常見的Span有:
AbsoluteSizeSpan(int size) ---- 設(shè)置字體大小,參數(shù)是絕對數(shù)值,相當(dāng)于Word中的字體大小RelativeSizeSpan(float proportion) ---- 設(shè)置字體大小,參數(shù)是相對于默認(rèn)字體大小的倍數(shù),比如默認(rèn)字體大小是x, 那么設(shè)置后的字體大小就是x*proportion,這個用起來比較靈活,proportion>1就是放大(zoom in), proportion<1就是縮小(zoom out)
ScaleXSpan(float proportion) ---- 縮放字體,與上面的類似,默認(rèn)為1,設(shè)置后就是原來的乘以proportion,大于1時放大(zoon in),小于時縮小(zoom out)BackgroundColorSpan(int color) ----背景著色,參數(shù)是顏色數(shù)值,可以直接使用android.graphics.Color里面定義的常量,或是用Color.rgb(int, int, int)ForegroundColorSpan(int color) ----前景著色,也就是字的著色,參數(shù)與背景著色一致TypefaceSpan(String family) ----字體,參數(shù)是字體的名字比如“sans", "sans-serif"等StyleSpan(Typeface style) -----字體風(fēng)格,比如粗體,斜體,參數(shù)是android.graphics.Typeface里面定義的常量,如Typeface.BOLD,Typeface.ITALIC等等。StrikethroughSpan----如果設(shè)置了此風(fēng)格,會有一條線從中間穿過所有的字,就像被劃掉一樣對于這些Sytle span在使用的時候通常只傳上面所說明的構(gòu)造參數(shù)即可,不需要設(shè)置其他的屬性,如果需要的話,也可以對它們設(shè)置其他的屬性。SpannableString和SpannableStringBuilder都有一個設(shè)置上述Span的方法:
/** * Set the style span to Spannable, such as SpannableString or SpannableStringBuilder * @param what --- the style span, such as StyleSpan * @param start --- the starting index of characters to which the style span to apply * @param end --- the ending index of characters to which the style span to apply * @param flags --- the flag specified to control */ setSpan(Object what, int start, int end, int flags);
其中參數(shù)what是要設(shè)置的Style span,start和end則是標(biāo)識String中Span的起始位置,而 flags是用于控制行為的,通常設(shè)置為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所在的端點這里理解起來就好像數(shù)學(xué)中定義區(qū)間,開區(qū)間還是閉區(qū)間一樣的。這里要重點說明下關(guān)于參數(shù)0,有很多時候,如果設(shè)置了上述的參數(shù),那么Span會從start應(yīng)用到Text結(jié)尾,而不是在start和end二者之間,這個時候就需要使用Flag 0。
在Textview中設(shè)置Drawable
類似調(diào)用方法如下:
1.在XML中使用android:drawableLeft="@drawable/icon"2.代碼中動態(tài)變化Drawable drawable= getResources().getDrawable(R.drawable.drawable);/// 這一步必須要做,否則不會顯示.drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());myTextview.setCompoundDrawables(drawable,null,null,null);也或參考另一個函數(shù)
public void setCompoundDrawablesWithIntrinsicBounds (Drawable left,Drawable top, Drawable right, Drawable bottom)
總結(jié)
以上是生活随笔為你收集整理的android常用字体代码,Android TextView设置字体风格多种组合的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Python从入门到数据分析第一篇—Py
- 下一篇: unity android录制视频教程,