日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

Android中实现不同文字颜色和图文混排的Span总结

發布時間:2025/3/17 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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版本的目的,并未使用,讀者也可以自行研究以下相關方法的使用

轉載于:https://www.cnblogs.com/txlbupt/p/3640826.html

與50位技術專家面對面20年技術見證,附贈技術全景圖

總結

以上是生活随笔為你收集整理的Android中实现不同文字颜色和图文混排的Span总结的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。