天天看點

TextView使用Spannable設定複合文本

端午過後又一天,還是沒事幹,再寫一篇部落格,過後就要期末考試了,可能最近就不出博文了,等暑假再重出江湖吧。

今天來弄弄這個textview的效果,應用場景還是很廣泛的,一個textview文本擁有各種各樣的樣式,以前給文本插入圖檔都是那種drawableleft啥的right的,圖檔大小還不好控制,排版又不好看,一次隻能插入一張圖檔,根本滿足不了需求,比如一些數學公式的上标和下标,純文字來打肯定是打不起來的,之前很早就看到spannable了,大概是14年暑假的時候玩emoj表情看到了這個功能,發現文本還能插入圖檔,感覺挺好玩的,時隔多年了沒有去管他,現在想想,有點後悔了,今天就來了解了解他吧。

spannable是個接口有兩個實作類,如下:

我們先來今天主要是看看spannablestring.class這個類,spannablestringbuilder 隻不過是用來整合spannablestring的各個效果,是以,關鍵還在于spannablestring的效果。

來看看api的解釋:

這個類最主要的一個設定效果的方法:

來看看解釋:

這裡呢,我們會通過這個方法來延伸今天的主題,我們先來看看方法最後一個參數flag的意思.

TextView使用Spannable設定複合文本

這個是api上面列出來的,我們今天主要講常用的四個,其他的大家自己去探索吧。

接下來是解釋這些用法,用有道翻譯了下,别介意^-^.

英文解釋:spans of type span_exclusive_exclusive do not expand to include text inserted at either their starting or ending point. they can never have a length of 0 and are automatically removed from the buffer if all the text they cover is removed.

中文解釋:如果start-end為0的話,插入就沒有效果,也就是上面說的:“他們永遠不會有一個長度為0會自動從緩沖區中删除”,如果要覆寫的話就包含他們的起點為0,end為文本長度,為:“如果删除它們覆寫所有的文本”。

英文解釋:non-0-length spans of type span_exclusive_inclusive expand to include text inserted at their ending point but not at their starting point. when 0-length, they behave like points.

中文解釋:non-0-length跨越span_exclusive_inclusive類型的擴充到包含文本插入到終點而不是他們的起點。當0-length時,他們表現得像點。

英文解釋:non-0-length spans of type span_inclusive_exclusive expand to include text inserted at their starting point but not at their ending point. when 0-length, they behave like marks.

中文解釋:non-0-length跨越span_inclusive_exclusive類型的擴充到包含文本插入到自己的起點而不是終點。當0-length,他們表現得像标志。

英文解釋:spans of type span_inclusive_inclusive expand to include text inserted at either their starting or ending point.

中文解釋:跨越span_inclusive_inclusive類型的擴充到包含文本插入他們的開始或結束。

好了,接下來就是看參數object what了,這個才是重頭戲呢,效果顯示都在這。

我們來看看這裡面都放的是啥,上面的解釋說了,裡面可以放characterstyle和paragraphstyle textwatcher和spanwatcher這四個類,我們主要來看看characterstyle.class這個類,我們來看看他的實作子類。

TextView使用Spannable設定複合文本

子類實作清單

接下來我們一一實驗看看效果。

spannablestring初始化代碼

backgroundcolorspan.class

TextView使用Spannable設定複合文本

foregroundcolorspan.class

<code>foregroundcolorspan fore=new foregroundcolorspan(0xff00ff00); str.setspan(fore, 3, 7, spannablestring.span_inclusive_inclusive);</code>

TextView使用Spannable設定複合文本

clickablespan.class

還有一種方式就是設定textview的屬性來設定可點選文本的樣式。

android:textcolorlink=”@drawable/icon”

要是想設定點選下去的樣式,就設定drawable為selector。

TextView使用Spannable設定複合文本

urlspan.class

這個效果圖就不貼了,和上面一樣,隻不過點選的時候,會觸發本地的浏覽器,打開一個網頁,這個拿來做公司介紹跳轉連結挺好用。

underlinespan.class

TextView使用Spannable設定複合文本

strikethroughspan.class

TextView使用Spannable設定複合文本

maskfilterspan.class

maskfilterspan這個類裡面接收一個maskfilter類,這個maskfilter類有三個子類,分别是:

blurmaskfilter.class://模糊效果

embossmaskfilter.class://浮雕效果

tablemaskfilter.class://–有道裡面沒有這個翻譯。。。。

TextView使用Spannable設定複合文本

absolutesizespan.class

該功能是實作字型的大小

TextView使用Spannable設定複合文本

localespan.class

用了下,不是有太多的明顯效果,這個類是用來設定地區語言的,看來的下api我感覺效果應該是為了差別各個國家對字元的處理不同吧,這是api上面翻譯:

—英文解釋:

for example, if you’re formatting integers some locales will use non-ascii decimal digits. as another example, if you’re formatting floating-point numbers some locales will use ‘,’ as the decimal point and ‘.’ for digit grouping. that’s correct for human-readable output, but likely to cause problems if presented to another computer (double.parsedouble(java.lang.string) can’t parse such a number, for example). you should also be wary of the string.tolowercase() and string.touppercase() overloads that don’t take a locale: in turkey, for example, the characters ‘i’ and ‘i’ won’t be converted to ‘i’ and ‘i’. this is the correct behavior for turkish text (such as user input), but inappropriate for, say, http headers.

— 中文解釋:

例如,如果你格式化整數一些地區将使用非ascii小數位數。另外一個例子,如果你格式化浮點數一些地區将使用”、“小數點和”。對數字分組。這對人類可讀的輸出是正确的,但可能導緻問題如果向另一台計算機(double.parsedouble(以)不能解析這樣的一個數字,例如)。你還應該警惕的string.tolowercase()和string.touppercase()過載,不要把一個語言環境:在土耳其,例如,人物“我”和“我”不會轉換為“我”和“我”。這是正确的行為對于土耳其文本(比如使用者輸入),但不适合,說,http頭。

imagespan.class

有些人使用imagespan的時候,喜歡給imagespan的構造函數直接傳入圖檔資源id,雖然是能插進去啊,但是你不能設定圖檔的大小,排版就一點都不好玩了

TextView使用Spannable設定複合文本

scalexspan.class

這個類的使用是對x軸方向的縮放,我之前還以為會有個y軸的縮放,但是好像沒看到,然後敲了一下這個y軸的類,還真沒有,最後我想了一下,字元串一般都是橫向寫入,要是再來個y軸方向的伸縮的話,豈不破壞了tetxview的height高度,是以google的工程師沒寫,我是這麼猜的。。。。

TextView使用Spannable設定複合文本

stylespan.class

typeface裡面有很多效果,你們自己嘗試嘗試

TextView使用Spannable設定複合文本

subscriptspan.class

TextView使用Spannable設定複合文本

textappearancespan.class

<code>textappearancespan textappearancespan = new textappearancespan(this,r.style.txtstyle); str.setspan(textappearancespan, 3, 7,spannablestring.span_exclusive_exclusive);</code>

這個類主要是用來引用style檔案裡面的格式集合,

TextView使用Spannable設定複合文本

typefacespan.class

這個類好像是用來設定字型的,看了下api給的說明,隻有三種字型

family - the font family for this typeface. examples include “monospace”, “serif”, and “sans-serif”.

但是我用了一下,好像沒啥效果,不知道啥意思,管他呢。

最後結束了,要用上整合整個格式的類spannablestringbuilder.class了

spannablestringbuilder.class

TextView使用Spannable設定複合文本

真的是實踐出真理哦,我前面所述的那個flag剛開始寫的時候我沒了解清楚,最後實踐的時候,我發現了問題,原來設定span_exclusive_exclusive,他的格式不會擴散給後面的文本,然而設定span_inclusive_inclusive時,後面的格式都被前面的格式又累加了一層上去,影響了後面的格式排版,錯誤示範給你們看看,我把上面的span_exclusive_exclusive全部換成span_inclusive_inclusive給你們看看效果圖。

TextView使用Spannable設定複合文本

全部替換後,後面的格全被影響了,一直加粗到後面,前面的下标也影響到後面的所有,然後接下來的斜體繼續影響後面,點選事件點選“越跑越6”也有了反應,是以,在做整合不同格式的時候最好使用span_exclusive_exclusive這個flag,我去,真是好險,還好弄懂了,學習還是不能唬自己啊。

好了好了,常見的說的差不多了,有些可能沒寫,要是有好的大家記得留言給我,我再來改改。

這次寫的博文雖然沒什麼技巧,隻是一些普通的使用流程而已,但是,卻收獲了不少,以前喜歡網上看别人的使用方法,沒有自己深入的去探索,現在呢,這次結合api和自己的實際操作,英語不好然後用有道來翻譯了一下,雖然時間用了好久,但是慢慢的,很享受這個過程,未來之際,希望再接再厲,麼麼哒

繼續閱讀