
androidTextView設置部分?字背景?和?字顏?
通過SpannableStringBuilder來實現,它就像html?邊的元素改變指定?字的?字顏?或背景?
復制代碼
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
te(savedInstanceState);
tContentView(ty_main);
String str="這是設置TextView部分?字背景顏?和前景顏?的demo!";
int bstart=f("背景");
int bend=bstart+"背景".length();
int fstart=f("前景");
int fend=fstart+"前景".length();
SpannableStringBuilder style=new SpannableStringBuilder(str);
n(new BackgroundColorSpan(),bstart,bend,_EXCLUSIVE_EXCLUSIVE);
n(new ForegroundColorSpan(),fstart,fend,_EXCLUSIVE_INCLUSIVE);
TextView tvColor=(TextView) findViewById(_color);
t(style);
}
}
復制代碼
AbsoluteSizeSpan(int size) —- 設置字體??,參數是絕對數值,相當于Word中的字體??RelativeSizeSpan(float proportion) —-
設置字體??,參數是相對于默認字體??的倍數,?如默認字體??是x, 那么設置后的字體??就是x*proportion,這個?起來?較靈
活,proportion>1就是放?(zoom in), proportion<1就是縮?(zoom out)
ScaleXSpan(float proportion) —- 縮放字體,與上?的類似,默認為1,設置后就是原來的乘以proportion,?于1時放?(zoon in),?
于時縮?(zoom out)
BackgroundColorSpan(int color) —-背景著?,參數是顏?數值,可以直接使???定義的常量,或是?
(int, int, int)
ForegroundColorSpan(int color) —-前景著?,也就是字的著?,參數與背景著??致TypefaceSpan(String family) —-字體,參數是
字體的名字?如“sans”, “sans-rif”等
StyleSpan(Typeface style) —–字體風格,?如粗體,斜體,參數是ce??定義的常量,如
,等等。StrikethroughSpan—-如果設置了此風格,會有?條線從中間穿過所有的字,就像被劃掉?
樣

本文發布于:2023-05-27 02:54:52,感謝您對本站的認可!
本文鏈接:http://m.newhan.cn/zhishi/a/168512729218726.html
版權聲明:本站內容均來自互聯網,僅供演示用,請勿用于商業和其他非法用途。如果侵犯了您的權益請與我們聯系,我們將在24小時內刪除。
本文word下載地址:androidTextView設置部分文字背景色和文字顏色.doc
本文 PDF 下載地址:androidTextView設置部分文字背景色和文字顏色.pdf
| 留言與評論(共有 0 條評論) |