1、怎麼在TextView中設置首行縮進兩個字符java
在string資源文件中,在文字的前面加入」\u3000\u3000」便可實現首行縮進android
在Java代碼中,使用setText("\u3000\u3000"+xxxxx);markdown
2、TextView中的圖文混排和不一樣顏色、大小字體的顯示app
方法一:設置不一樣顏色、大小、圖文混排的效果經過SpannableString,而且設置各類Span實現的。ide
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類型爲:spa
ImageSpan 可使用圖片替換文字達到圖文混排的效果,例如在通常聊天工具當中在文字和表情一塊兒發的狀態。code
使用方法爲:orm
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版本的目的,並未使用,讀者也能夠自行研究如下相關方法的使用