通常用於顯示文字,但有時候也需要在文字中夾雜一些圖片,比如QQ中就可以使用表情圖片,又比如需要的文字高亮顯示等等,如何在android中也做到這樣呢?
記得android中有個android.text包,這裏提供了對文本的強大的處理功能。
添加圖片主要用SpannableString和ImageSpan類:
Drawable drawable = getResources().getDrawable(id);
drawable.setBounds(0, 0,
drawable.getIntrinsicWidth(),
drawable.getIntrinsicHeight());
//需要處理的文本,[smile]是需要被替代的文本
SpannableString spannable = new SpannableString(getText().toString()
+"[smile]");
//要讓圖片替代指定的文字就要用ImageSpan
ImageSpan span = new ImageSpan(drawable, ImageSpan.ALIGN_BASELINE);
//開始替換,注意第2和第3個參數表示從哪裏開始替換到哪裏替換結束(start和end)
//最後一個參數類似數學中的集合,[5,12)表示從5到12,包括5但不包括12
spannable.setSpan(span, getText().length(),getText().length()
+"[smile]".length(), Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
setText(spannable);
有的時候可能只想用一個textview來表示文字,但文字裏肯定有分不同內容的東西,需要用不同的顏色、樣式來表示.
這個時候,就需要用到SpannableString對象來處理。
/**
* new StrikethroughSpan() //設置刪除線
* new URLSpan("http://www.baidu.com")//http超鏈接
* new URLSpan("tel:4155551212")//電話超鏈接
*/
mTextView = (TextView)findViewById(R.id.test);
SpannableString tSS = new SpannableString(“SpannableString學習中”);
tSS.setSpan(new BackgroundColorSpan(Color.RED), 0, 2, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); //紅色高亮
tSS.setSpan(new UnderlineSpan(), 15, 18,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); //下劃線
tSS.setSpan(new StyleSpan(android.graphics.Typeface.BOLD_ITALIC), 2, 5, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); //斜體
mTextView.setText(tSS);
關鍵方法:
public void setSpan (Object what, int start, int end, int flags)
start是起始位置,無論中英文,都算一個。從0開始計算起。end是結束位置,所以處理的文字,包含開始位置,但不包含結束位置。
public void highlight(int start,int end){
SpannableStringBuilder spannable=
new SpannableStringBuilder(getText().toString());//用於可變字符串
ForegroundColorSpan span=new ForegroundColorSpan(Color.RED);
spannable.setSpan(span, start,
end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
setText(spannable);
}
加下劃線:
public void underline(int start,int end){
SpannableStringBuilder spannable=
new SpannableStringBuilder(getText().toString());
CharacterStyle span=new UnderlineSpan();
spannable.setSpan(span, start,
end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
setText(spannable);
}
組合運用:
SpannableStringBuilder spannable=
new SpannableStringBuilder(getText().toString());
CharacterStyle span_1=new StyleSpan(android.graphics.Typeface.ITALIC);
CharacterStyle span_2=new ForegroundColorSpan(Color.RED);
spannable.setSpan(span_1, start,
end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
spannable.setSpan(span_2, start,
end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
setText(spannable);
案例:帶有\n換行符的字符串都可以用此方法顯示2種顏色
/**
* 帶有\n換行符的字符串都可以用此方法顯示2種顏色
* @param text
* @param color1
* @param color2
* @return
*/
public SpannableStringBuilder highlight(String text,int color1,int color2,int fontSize){
SpannableStringBuilder spannable=new SpannableStringBuilder(text);//用於可變字符串
CharacterStyle span_0=null,span_1=null,span_2;
int end=text.indexOf("\n");
if(end==-1){//如果沒有換行符就使用第一種顏色顯示
span_0=new ForegroundColorSpan(color1);
spannable.setSpan(span_0, 0,
text.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}else{
span_0=new ForegroundColorSpan(color1);
span_1=new ForegroundColorSpan(color2);
spannable.setSpan(span_0, 0,
end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
spannable.setSpan(span_1, end+1,
text.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
span_2=new AbsoluteSizeSpan(fontSize);//字體大小
spannable.setSpan(span_2, end+1,
text.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
return spannable;
}
字體背景顏色(BackgroundColorSpan)
SpannableString spanString = new SpannableString("歡迎光臨Harvic的博客");
BackgroundColorSpan span = new BackgroundColorSpan(Color.YELLOW);
spanString.setSpan(span, 0, 3, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
editText.setText(spanString);
以上實際都是Html.fromHtml(html)的代碼實現形式。
=============================================================================
TextView: (方法與EditView類似)
如何讓一個TextView中的關鍵字高亮顯示?
/**
* 關鍵字高亮顯示
* @param target 需要高亮的關鍵字
*/
public void highlight(String target){
String temp=getText().toString();
SpannableStringBuilder spannable = new SpannableStringBuilder(temp);
CharacterStyle span=null;
Pattern p = Pattern.compile(target);
Matcher m = p.matcher(temp);
while (m.find()) {
span = new ForegroundColorSpan(Color.RED);//需要重複!
//span = new ImageSpan(drawable,ImageSpan.XX);//設置現在圖片
spannable.setSpan(span, m.start(), m.end(),
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
setText(spannable);
}
本文出自:南小爵的博客