TextView通常用來顯示普通文本,但是有時候需要對其中某些文本進行樣式、事件方面的設置。Android系統通過SpannableString類來對指定文本進行相關處理,實際應用中用的比較多的地方比如聊天時顯示表情啊,朋友圈或社區中話題的顯示、@好友顯示和點擊等等,關鍵字顯示不同顏色……
1、BackgroundColorSpan 背景色
SpannableString spanText = new SpannableString("BackgroundColorSpan"); spanText.setSpan(new BackgroundColorSpan(Color.GREEN), 0, spanText.length(), Spannable.SPAN_INCLUSIVE_EXCLUSIVE); mTVText.append("/n"); mTVText.append(spanText);2、ClickableSpan 文本可點擊,有點擊事件
spannableString.setSpan(new ClickableSpan() { @Override public void onClick(View widget) { spanClickListener.onSpanClick(bean); } @Override public void updateDrawState(TextPaint ds) { super.updateDrawState(ds); //設置畫筆屬性 ds.setUnderlineText(false);//默認有下劃線 } }, matcher.start(), end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);tvTopic.setText(spannableString);//如果想實現點擊,必須要設置這個tvTopic.setMovementMethod(LinkMovementMethod.getInstance());3、ForegroundColorSpan 文本顏色(前景色)
spanText = new SpannableString("這是ForegroundColorSpan,看到了嗎");spanText.setSpan(new ForegroundColorSpan(Color.BLUE), 6, spanText.length(),Spannable.SPAN_INCLUSIVE_EXCLUSIVE);mTVText.append("/n");mTVText.append(spanText);4、MaskFilterSpan 修飾效果,如模糊(BlurMaskFilter)、浮雕(EmbossMaskFilter)
spanText = new SpannableString("這是MaskFilterSpan,,看到了嗎");int length = spanText.length();//模糊(BlurMaskFilter)MaskFilterSpan maskFilterSpan = new MaskFilterSpan(new BlurMaskFilter(3, Blur.OUTER));spanText.setSpan(maskFilterSpan, 0, length - 10, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);//浮雕(EmbossMaskFilter)maskFilterSpan = new MaskFilterSpan(new EmbossMaskFilter(new float[]{1,1,3}, 1.5f, 8, 3));spanText.setSpan(maskFilterSpan, length - 10, length, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);mTVText.append("/n");mTVText.append(spanText);7、StrikethroughSpan 刪除線(中劃線)
spanText = new SpannableString("StrikethroughSpan");spanText.setSpan(new StrikethroughSpan(), 0, 7, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);mTVText.append("/n");mTVText.append(spanText);8、SuggestionSpan
相當于占位符,一般用在EditText輸入框中。當雙擊此文本時,會彈出提示框選擇一些建議(推薦的)文字,選中的文本將替換此占位符。在輸入法上用的較多。
9、UnderlineSpan 下劃線
spanText = new SpannableString("UnderlineSpan");spanText.setSpan(new UnderlineSpan(), 0, spanText.length(),Spannable.SPAN_INCLUSIVE_EXCLUSIVE);mTVText.append("/n");mTVText.append(spanText);10、AbsoluteSizeSpan 絕對大小(文本字體)
spanText = new SpannableString("AbsoluteSizeSpan");spanText.setSpan(new AbsoluteSizeSpan(20, true), 0, spanText.length(),Spannable.SPAN_INCLUSIVE_EXCLUSIVE);mTVText.append("/n");mTVText.append(spanText);11、DynamicDrawableSpan 設置圖片,基于文本基線或底部對齊。
DynamicDrawableSpan drawableSpan = new DynamicDrawableSpan(DynamicDrawableSpan.ALIGN_BASELINE) { @Override public Drawable getDrawable() { Drawable d = getResources().getDrawable(R.drawable.ic_launcher); d.setBounds(0, 0, 50, 50); return d; }};DynamicDrawableSpan drawableSpan2 = new DynamicDrawableSpan(DynamicDrawableSpan.ALIGN_BOTTOM) { @Override public Drawable getDrawable() { Drawable d = getResources().getDrawable(R.drawable.ic_launcher); d.setBounds(0, 0, 50, 50); return d; } };spanText.setSpan(drawableSpan, 3, 4, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);spanText.setSpan(drawableSpan2, 7, 8, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);mTVText.append("/n");mTVText.append(spanText);12、ImageSpan 圖片
spanText = new SpannableString("ImageSpan");Drawable d = getResources().getDrawable(R.drawable.ic_launcher);d.setBounds(0, 0, 50, 50);spanText.setSpan(new ImageSpan(d), 3, 4, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);mTVText.append("/n");mTVText.append(spanText);13、RelativeSizeSpan 相對大小(文本字體)
spanText = new SpannableString("RelativeSizeSpan");//參數proportion:比例大小spanText.setSpan(new RelativeSizeSpan(2.5f), 3, 4,Spannable.SPAN_INCLUSIVE_EXCLUSIVE);mTVText.append("/n");mTVText.append(spanText);14、ScaleXSpan 基于x軸縮放
spanText = new SpannableString("ScaleXSpan");//參數proportion:比例大小spanText.setSpan(new ScaleXSpan(3.8f), 3, 7, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);mTVText.append("/n");mTVText.append(spanText);15、StyleSpan 字體樣式:粗體、斜體等
spanText = new SpannableString("StyleSpan");spanText.setSpan(new StyleSpan(Typeface.BOLD_ITALIC), 3, 7,Spannable.SPAN_INCLUSIVE_EXCLUSIVE);mTVText.append("/n");mTVText.append(spanText);16、SubscriptSpan 下標(數學公式會用到)
spanText = new SpannableString("SubscriptSpan");spanText.setSpan(new SubscriptSpan(), 6, 7, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);mTVText.append("/n");mTVText.append(spanText);17、SuperscriptSpan 上標(數學公式會用到)
spanText = new SpannableString("SuperscriptSpan");spanText.setSpan(new SuperscriptSpan(), 6, 7, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);mTVText.append("/n");mTVText.append(spanText);18、TextAppearanceSpan 文本外貌(包括字體、大小、樣式和顏色)
spanText = new SpannableString("TextAppearanceSpan");//若需自定義TextAppearance,可以在系統樣式上進行修改spanText.setSpan(new TextAppearanceSpan(this, android.R.style.TextAppearance_Medium), 6, 7, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);mTVText.append("/n");mTVText.append(spanText);19、TypefaceSpan 文本字體
spanText = new SpannableString("TypefaceSpan");spanText.setSpan(new TypefaceSpan("monospace"), 3, 10,Spannable.SPAN_INCLUSIVE_EXCLUSIVE);mTVText.append("/n");mTVText.append(spanText);20、URLSpan 文本超鏈接
spanText = new SpannableString("URLSpan");spanText.setSpan(new URLSpan("http://blog.csdn.net/u011102153"), 10, spanText.length(),Spannable.SPAN_INCLUSIVE_EXCLUSIVE);mTVText.append("/n");mTVText.append(spanText);//讓URLSpan可以點擊mTVText.setMovementMethod(new LinkMovementMethod());下載:https://github.com/LineChen/SpannableStringDemo
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。
新聞熱點
疑難解答