日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

Android TextView 设置文字背景色或文字颜色,字体阴影,字体样式

發布時間:2023/12/10 Android 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Android TextView 设置文字背景色或文字颜色,字体阴影,字体样式 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

??????String str="這是設置TextView部分文字背景顏色和前景顏色的demo!";

????????int?bstart=str.indexOf("背景");

????????int?bend=bstart+"背景".length();

????????int?fstart=str.indexOf("前景");

????????int?fend=fstart+"前景".length();

????????SpannableStringBuilder style=new?SpannableStringBuilder(str);

????????style.setSpan(new?BackgroundColorSpan(Color.RED),bstart,bend,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);??

????????style.setSpan(new?ForegroundColorSpan(Color.RED),fstart,fend,Spannable.SPAN_EXCLUSIVE_INCLUSIVE);

????????TextView tvColor=(TextView) findViewById(R.id.tv_color);

????????tvColor.setText(style);

?

動態TextView文本內容:

<string name="str_text1">上期抄表%1$s度,本期抄表%2$s度</string> textView1.setText(getResources().getString(R.string.str_text1,120+"",200+""));

?

private void setSpann(){//1ForegroundColorSpan 文本顏色(前景色)SpannableString spannableString = new SpannableString("我是一個無聊的文本,啦啦啦啦啦啦");ForegroundColorSpan colorSpan = new ForegroundColorSpan(Color.parseColor("#0099EE"));spannableString.setSpan(colorSpan, 9, spannableString.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);textView3.append("\n1");textView3.setText(spannableString);//2BackgroundColorSpan 背景色spannableString.setSpan(new BackgroundColorSpan(Color.parseColor("#0099EE")), 6, spannableString.length(), spannableString.SPAN_INCLUSIVE_EXCLUSIVE);textView3.append("\n2");textView3.append(spannableString);// 3. ClickableSpan: 點擊事件相關的Span。ClickableSpan clickableSpan = new ClickableSpan() {@Overridepublic void onClick(View widget) {Toast.makeText(MainActivity.this, "click", Toast.LENGTH_SHORT).show();Log.e("Easy", "click");}};spannableString.setSpan(clickableSpan, 7, 11, Spannable.SPAN_INCLUSIVE_INCLUSIVE) ;textView3.setMovementMethod(LinkMovementMethod.getInstance());textView3.append("\n3");textView3.append(spannableString);//4MaskFilterSpan 修飾效果,如模糊(BlurMaskFilter)、浮雕(EmbossMaskFilter)int length = spannableString.length();//模糊(BlurMaskFilter)MaskFilterSpan maskFilterSpan = new MaskFilterSpan(new BlurMaskFilter(3, BlurMaskFilter.Blur.OUTER));spannableString.setSpan(maskFilterSpan, 0, length - 10, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);//浮雕(EmbossMaskFilter)maskFilterSpan = new MaskFilterSpan(new EmbossMaskFilter(new float[]{1,1,3}, 1.5f, 8, 3));spannableString.setSpan(maskFilterSpan, length - 10, length, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);textView3.append("\n4");textView3.append(spannableString);// 5、MetricAffectingSpan 父類,一般不用// RasterizerSpan 光柵效果 // spannableString.setSpan(new RasterizerSpan(), 0, 7, Spannable. // SPAN_INCLUSIVE_EXCLUSIVE); // textView3.append("\n"); // textView3.append(spannableString);// 7StrikethroughSpan 刪除線(中劃線)spannableString.setSpan(new StrikethroughSpan(), 0, 7, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);textView3.append("\n7");textView3.append(spannableString);// 8、SuggestionSpan // 相當于占位符,一般用在EditText輸入框中。當雙擊此文本時,會彈出提示框選擇一些建議(推薦的)文字,選中的文本將替換此占位符。在輸入法上用的較多。 // PS:API 14新增,暫無示例。//9UnderlineSpan 下劃線spannableString.setSpan(new UnderlineSpan(), 0, spannableString.length(),Spannable.SPAN_INCLUSIVE_EXCLUSIVE);textView3.append("\n9");textView3.append(spannableString);// 10AbsoluteSizeSpan 絕對大小(文本字體)spannableString.setSpan(new AbsoluteSizeSpan(20, true), 0, 7,Spannable.SPAN_INCLUSIVE_EXCLUSIVE);textView3.append("\n10");textView3.append(spannableString);// 11、DynamicDrawableSpan 設置圖片,基于文本基線或底部對齊。DynamicDrawableSpan drawableSpan =new DynamicDrawableSpan(DynamicDrawableSpan.ALIGN_BASELINE) {@Overridepublic Drawable getDrawable() {Drawable d = getResources().getDrawable(R.mipmap.ic_launcher);d.setBounds(0, 0, 50, 50);return d;}};DynamicDrawableSpan drawableSpan2 = new DynamicDrawableSpan(DynamicDrawableSpan.ALIGN_BOTTOM) {@Overridepublic Drawable getDrawable() {Drawable d = getResources().getDrawable(R.mipmap.ic_launcher);d.setBounds(0, 0, 50, 50);return d;}};spannableString.setSpan(drawableSpan, 3, 4, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);spannableString.setSpan(drawableSpan2, 7, 8, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);textView3.append("\n11");textView3.append(spannableString);// 12ImageSpan 圖片Drawable d = getResources().getDrawable(R.mipmap.ic_launcher);d.setBounds(0, 0, 50, 50);spannableString.setSpan(new ImageSpan(d), 3, 4, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);textView3.append("\n12");textView3.append(spannableString);//13、RelativeSizeSpan 相對大小(文本字體)//參數proportion:比例大小spannableString.setSpan(new RelativeSizeSpan(2.5f), 3, 4,Spannable.SPAN_INCLUSIVE_EXCLUSIVE);textView3.append("\n13");textView3.append(spannableString);// 14、ReplacementSpan 父類,一般不用// 15、ScaleXSpan 基于x軸縮放// 參數proportion:比例大小spannableString.setSpan(new ScaleXSpan(3.8f), 3, 7, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);textView3.append("\n15");textView3.append(spannableString);// 16、StyleSpan 字體樣式:粗體、斜體等//Typeface.BOLD_ITALIC:粗體+斜體spannableString.setSpan(new StyleSpan(Typeface.BOLD), 3, 7,Spannable.SPAN_INCLUSIVE_EXCLUSIVE);textView3.append("\n16");textView3.append(spannableString);// 17、SubscriptSpan 下標(數學公式會用到)spannableString.setSpan(new SubscriptSpan(), 6, 7, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);textView3.append("\n17");textView3.append(spannableString);// 18、SuperscriptSpan 上標(數學公式會用到)spannableString.setSpan(new SuperscriptSpan(), 6, 7, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);textView3.append("\n18");textView3.append(spannableString);// 19、TextAppearanceSpan 文本外貌(包括字體、大小、樣式和顏色)//若需自定義TextAppearance,可以在系統樣式上進行修改spannableString.setSpan(new TextAppearanceSpan(this, android.R.style.TextAppearance_Medium),6, 7, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);textView3.append("\n19");textView3.append(spannableString);// 20、TypefaceSpan 文本字體//若需使用自定義字體,可能要重寫類TypefaceSpanspannableString.setSpan(new TypefaceSpan("monospace"), 3, 10,Spannable.SPAN_INCLUSIVE_EXCLUSIVE);textView3.append("\n20");textView3.append(spannableString);// 21、URLSpan 文本超鏈接spannableString.setSpan(new URLSpan("http://orgcent.com"), 10, spannableString.length(),Spannable.SPAN_INCLUSIVE_EXCLUSIVE);textView3.setMovementMethod(new LinkMovementMethod());textView3.append("\n21");textView3.append(spannableString);//讓URLSpan可以點擊}

部分字體著色

代碼:

String sms = "審計系統在線"; String sjs = "審計系統在線"; int fstart = 4; int fend = 6; if (sms.contains("在線")) {SpannableStringBuilder style = new SpannableStringBuilder(sms);style.setSpan(new ForegroundColorSpan(Color.rgb(166, 116, 50)), fstart, fend, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);nethomesmstatus.setText(style); } else {nethomesmstatus.setText(sms); }

?

效果:

?

?

字體陰影

textView.setShadowLayer(2,6,20,R.color.colorPrimary);//清晰度,偏移量,最后一個無效參數

?

字體樣式

字體類型
Typeface.DEFAULT:默認字體,常規字體類型
Typeface.DEFAULT_BOLD:黑體字體類型
Typeface.MONOSPACE:等寬字體類型
Typeface.SANS_SERIF:sans serif字體類型
字體樣式
Typeface.BOLD //粗體
Typeface.BOLD_ITALIC //粗斜體
Typeface.ITALIC //斜體
Typeface.NORMAL //常規
?

?

textView.setTypeface(Typeface.DEFAULT, Typeface.BOLD_ITALIC );

?

?//下劃線?

textView.getPaint().setFlags(Paint. UNDERLINE_TEXT_FLAG );

//抗鋸齒

textView.getPaint().setAntiAlias(true);

//中劃線
textview.getPaint().setFlags(Paint. STRIKE_THRU_TEXT_FLAG);?

// 設置中劃線并加清晰?
textview.setFlags(Paint. STRIKE_THRU_TEXT_FLAG|Paint.ANTI_ALIAS_FLAG); ?

?

holder.odlte.setPaintFlags(Paint. STRIKE_THRU_TEXT_FLAG|Paint.ANTI_ALIAS_FLAG);

//字體加粗

textView.getPaint().setFakeBoldText(true);
?

總結

以上是生活随笔為你收集整理的Android TextView 设置文字背景色或文字颜色,字体阴影,字体样式的全部內容,希望文章能夠幫你解決所遇到的問題。

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