編輯:關於Android編程
在使用TextView的過程中,有時候會需要將一串文本中的部分文字做特別的顯示效果處理,比如加粗、改變顏色、加著重標識、超鏈接等等,我們可以通過多個TextView拼湊來實現,但如果需要特殊處理的內容在整個文本的中間,用多個TextView拼湊實在是太麻煩、太Low了,Android提供的SpannableString類能夠很好地解決這個問題,SpannableString結合下面各種以Span結尾的類可以做出豐富多樣的顯示效果。
本文結合平時工作情況,寫了一個SpannableUtils工具類,方便今後使用,SpannableUtils.java具體代碼如下,如果要對同一串文本同時設置多種顯示效果,可以考慮改造該類實現:
/** * Spannable工具類,用於設置文字的前景色、背景色、Typeface、粗體、斜體、字號、超鏈接、刪除線、下劃線、上下標等 * * */ public class SpannableUtils { private SpannableUtils( ){ } /** * 改變字符串中某一段文字的字號 * setTextSize("",24,0,2) = null; * setTextSize(null,24,0,2) = null; * setTextSize("abc",-2,0,2) = null; * setTextSize("abc",24,0,4) = null; * setTextSize("abc",24,-2,2) = null; * setTextSize("abc",24,0,2) = normal string * */ public static SpannableString setTextSize( String content, int startIndex, int endIndex, int fontSize ){ if( TextUtils.isEmpty( content ) || fontSize <= 0 || startIndex >= endIndex || startIndex < 0 || endIndex >= content.length( ) ){ return null; } SpannableString spannableString = new SpannableString( content ); spannableString.setSpan( new AbsoluteSizeSpan( fontSize ), startIndex, endIndex, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE ); return spannableString; } public static SpannableString setTextSub( String content, int startIndex, int endIndex ){ if( TextUtils.isEmpty( content ) || startIndex < 0 || endIndex >= content.length( ) || startIndex >= endIndex ){ return null; } SpannableString spannableString = new SpannableString( content ); spannableString.setSpan( new SubscriptSpan( ), startIndex, endIndex, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE ); return spannableString; } public static SpannableString setTextSuper( String content, int startIndex, int endIndex ){ if( TextUtils.isEmpty( content ) || startIndex < 0 || endIndex >= content.length( ) || startIndex >= endIndex ){ return null; } SpannableString spannableString = new SpannableString( content ); spannableString.setSpan( new SuperscriptSpan( ), startIndex, endIndex, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE ); return spannableString; } public static SpannableString setTextStrikethrough( String content, int startIndex, int endIndex ){ if( TextUtils.isEmpty( content ) || startIndex < 0 || endIndex >= content.length( ) || startIndex >= endIndex ){ return null; } SpannableString spannableString = new SpannableString( content ); spannableString.setSpan(new StrikethroughSpan(), startIndex, endIndex, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); return spannableString; } public static SpannableString setTextUnderline( String content, int startIndex, int endIndex ){ if( TextUtils.isEmpty( content ) || startIndex < 0 || endIndex >= content.length( ) || startIndex >= endIndex ){ return null; } SpannableString spannableString = new SpannableString( content ); spannableString.setSpan(new UnderlineSpan(), startIndex, endIndex, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); return spannableString; } public static SpannableString setTextBold( String content, int startIndex, int endIndex ){ if( TextUtils.isEmpty( content ) || startIndex < 0 || endIndex >= content.length( ) || startIndex >= endIndex ){ return null; } SpannableString spannableString = new SpannableString( content ); spannableString.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), startIndex, endIndex, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); return spannableString; } public static SpannableString setTextItalic( String content, int startIndex, int endIndex ){ if( TextUtils.isEmpty( content ) || startIndex < 0 || endIndex >= content.length( ) || startIndex >= endIndex ){ return null; } SpannableString spannableString = new SpannableString( content ); spannableString.setSpan(new StyleSpan(android.graphics.Typeface.ITALIC), startIndex, endIndex, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); return spannableString; } public static SpannableString setTextBoldItalic( String content, int startIndex, int endIndex ){ if( TextUtils.isEmpty( content ) || startIndex < 0 || endIndex >= content.length( ) || startIndex >= endIndex ){ return null; } SpannableString spannableString = new SpannableString( content ); spannableString.setSpan(new StyleSpan(android.graphics.Typeface.BOLD_ITALIC), startIndex, endIndex, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); return spannableString; } public static SpannableString setTextForeground( String content, int startIndex, int endIndex, int foregroundColor ){ if( TextUtils.isEmpty( content ) || startIndex < 0 || endIndex >= content.length( ) || startIndex >= endIndex ){ return null; } SpannableString spannableString = new SpannableString( content ); spannableString.setSpan(new ForegroundColorSpan( foregroundColor ), startIndex, endIndex, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); return spannableString; } public static SpannableString setTextBackground( String content, int startIndex, int endIndex, int backgroundColor ){ if( TextUtils.isEmpty( content ) || startIndex < 0 || endIndex >= content.length( ) || startIndex >= endIndex ){ return null; } SpannableString spannableString = new SpannableString( content ); spannableString.setSpan(new BackgroundColorSpan( backgroundColor ), startIndex, endIndex, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); return spannableString; } /** * 設置文本的超鏈接 * @param content 需要處理的文本 * @param startIndex * @param endIndex 被處理文本中需要處理字串的開始和結束索引 * @param url 文本對應的鏈接地址,需要注意格式: * (1)電話以"tel:"打頭,比如"tel:02355692427" * (2)郵件以"mailto:"打頭,比如"mailto:[email protected]" * (3)短信以"sms:"打頭,比如"sms:02355692427" * (4)彩信以"mms:"打頭,比如"mms:02355692427" * (5)地圖以"geo:"打頭,比如"geo:68.426537,68.123456" * (6)網絡以"http://"打頭,比如"http://www.google.com" * */ public static SpannableString setTextURL( String content, int startIndex, int endIndex, String url ){ if( TextUtils.isEmpty( content ) || startIndex < 0 || endIndex >= content.length( ) || startIndex >= endIndex ){ return null; } SpannableString spannableString = new SpannableString( content ); spannableString.setSpan(new URLSpan( url ), startIndex, endIndex, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); return spannableString; } public static SpannableString setTextImg( String content, int startIndex, int endIndex, Drawable drawable ){ if( TextUtils.isEmpty( content ) || startIndex < 0 || endIndex >= content.length( ) || startIndex >= endIndex ){ return null; } SpannableString spannableString = new SpannableString( content ); spannableString.setSpan(new ImageSpan(drawable), startIndex, endIndex, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); return spannableString; } }
本例Demo可以在此下載:Spannable實例
Android Studio在打開的文件左側單擊鼠標右鍵,也能像Eclipse一樣設置顯示代碼行數,如圖1。但是這邊跟Eclipse有一個很大的區別,Eclipse設置後
使用起來超簡單,如圖: Android Button Maker is online tool to generate button
這次我們試水高德LBS開放平台,那麼,什麼是LBS?基於位置的服務,它是通過電信移動運營商的無線電通訊網絡(如GSM網、CDMA網)或外部定位方式(如GPS)獲取移動終端
微信可以辦護照、續簽港澳通行證,只有1%的人知道!你也趕快來學習吧,我們是及時update的小伙伴。辦護照、續簽港澳通行證功能,對於需要經常出行出差的朋友來