Android中實現不同文字顏色和圖文混排的Span總結


一、怎么在TextView中設置首行縮進兩個字符

 

在string資源文件中,在文字的前面加入”\u3000\u3000”即可實現首行縮進

在Java代碼中,使用setText("\u3000\u3000"+xxxxx);

 

二、TextView中的圖文混排和不同顏色、大小字體的顯示

方法一:設置不同顏色、大小、圖文混排的效果通過SpannableString,並且設置各種Span實現的。

SpannableString的setSpan方法需要幾個參數:

public void setSpan (Object what, int start, int end, int flags)

what傳入各種Span類型的實例,start和end標記要替代的文字內容的范圍,flags是用來標識在 Span 范圍內的文本前后輸入新的字符時是否把它們也應用這個效果,可以傳入Spanned.SPAN_EXCLUSIVE_EXCLUSIVE、Spanned.SPAN_INCLUSIVE_EXCLUSIVE、Spanned.SPAN_EXCLUSIVE_INCLUSIVE、Spanned.SPAN_INCLUSIVE_INCLUSIVE幾個參數,INCLUSIVE表示應用該效果,EXCLUSIVE表示不應用該效果,如Spanned.SPAN_INCLUSIVE_EXCLUSIVE表示對前面的文字應用該效果,而對后面的文字不應用該效果。

可以使用的主要幾種Span類型為:

ImageSpan 可以使用圖片替換文字達到圖文混排的效果,例如在一般聊天工具當中在文字和表情一起發的狀態。

使用方法為:

Drawabledrawable=mContext.getResources().getDrawable(R.drawable.new_topic_drawable);

drawable.setBounds(0,0,drawable.getIntrinsicWidth(),drawable.getIntrinsicHeight());

ImageSpan imageSpan = new ImageSpan(drawable, ImageSpan.ALIGN_BASELINE);

spanString.setSpan(imageSpan,spanString.length()-1,spanString.length(),Spannable.SPAN_INCLUSIVE_INCLUSIVE);

 

ForegroundColorSpan 設置文字前景色,即文字本身的顏色

spanString.setSpan(new ForegroundColorSpan(Color.parseColor("#f74224")), 0,titleText.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);

AbsoluteSizeSpan 設置文字的絕對大小值

spanString.setSpan(new AbsoluteSizeSpan(11),0,spanString.length(),titleText.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);

UrlSpan 設置超鏈接

URLSpan span = new URLSpan("tel:0123456789");

spanString.setSpan(span, 0, 3, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

BackgroundColorSpan 設置文字背景色

BackgroundColorSpan span = new BackgroundColorSpan(Color.YELLOW);

spanString.setSpan(span, 0, 3, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

StyleSpan 字體設置

StyleSpan span = new StyleSpan(Typeface.BOLD_ITALIC);

spanString.setSpan(span, 0, 2, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

Typeface中有四個Style常量,分別是BOLD粗體、ITALIC斜體、BOLD_ITALIC粗斜體、NORMAL正常

StrikethroughSpan 刪除線

StrikethroughSpan span = new StrikethroughSpan();

spanString.setSpan(span, 0, 3, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

UnderlineSpan下划線

UnderlineSpan span = new UnderlineSpan();

spanString.setSpan(span, 0, 3, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

在具體實踐中可以對同一范圍內的文字疊加不同的span,如文字的大小和文字的顏色可以疊加使用,可以組合出不同的效果

 

此外在API 17中,TextView自帶了幾個方法也可以達到圖文混排的效果:

public void setCompoundDrawablesRelative (Drawable start, Drawable top, Drawable end, Drawable bottom)

Sets the Drawables (if any) to appear to the start of, above, to the end of, and below the text. Use null if you do not want a Drawable there. The Drawables must already have had setBounds(Rect) called.

Related XML Attributes
android:drawableStart
android:drawableTop
android:drawableEnd
android:drawableBottom
public void setCompoundDrawablesRelativeWithIntrinsicBounds (Drawable start, Drawable top, Drawable end, Drawable bottom) Sets the Drawables (if any) to appear to the start of, above, to the end of, and below the text. Use null if you do not want a Drawable there. The Drawables' bounds will be set to their intrinsic bounds. Related XML Attributes android:drawableStart android:drawableTop android:drawableEnd android:drawableBottom
public void setCompoundDrawablesRelativeWithIntrinsicBounds (int start, int top, int end, int bottom)
Sets the Drawables (if any) to appear to the start of, above, to the end of, and below the text. Use 0 if you do not want a Drawable there. The Drawables' bounds will be set to their intrinsic bounds. Related XML Attributes android:drawableStart android:drawableTop android:drawableEnd android:drawableBottom Parameters start Resource identifier of the start Drawable. top Resource identifier of the top Drawable. end Resource identifier of the end Drawable. bottom Resource identifier of the bottom Drawable.

由於項目中要達到兼容2.3.x版本的目的,並未使用,讀者也可以自行研究以下相關方法的使用


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM