Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android:通過SpannableString為TextView設置豐富的顯示效果

Android:通過SpannableString為TextView設置豐富的顯示效果

編輯:關於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效果:



本例Demo可以在此下載:Spannable實例

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved