android實現(xiàn)風(fēng)格文本

在平時的開發(fā)中經(jīng)常遇到文本中帶有數(shù)字或者字母之類改變文本顏色添加下劃線,或在文本左側(cè)、右側(cè)添加圖片等需求棒掠,這里自己寫了個非常實用的類,定義顏色圖片等(支持多規(guī)則)

首先看效果圖:



自定義DiyStyleTextView繼承自AppCompatTextView

文本是否顯示下劃線

public DiyStyleTextViewsetUnderlineText(boolean underlineText) {

this.underlineText = underlineText;

return this;

}

添加自定義顏色規(guī)則屁商,滿足該正則表達(dá)式的文本自定義顏色烟很,colorRegex正則表達(dá)式,color自定義的顏色

public DiyStyleTextViewaddColorRegex(String colorRegex, int color) {

setMovementMethod(LinkMovementMethod.getInstance());

? ? colorRegexList.add(colorRegex);

? ? colorList.add(color);

return this;

}

添加自定義圖片規(guī)則

public DiyStyleTextViewaddImageRegex(String imageRegex, Bitmap bitmap) {

setMovementMethod(LinkMovementMethod.getInstance());

? ? imageRegexList.add(imageRegex);

? ? bitmapList.add(bitmap);

return this;

}

最后貼上代碼:

public class DiyStyleTextViewextends AppCompatTextView {

private boolean underlineText =false;

? ? private ListcolorRegexList =new ArrayList<>();

? ? private ListcolorList =new ArrayList<>();

? ? private ListimageRegexList =new ArrayList<>();

? ? private ListbitmapList =new ArrayList<>();

? ? public DiyStyleTextView(Context context) {

super(context);

? ? }

public DiyStyleTextView(Context context, AttributeSet attrs) {

super(context, attrs);

? ? }

//是否顯示下劃線-全局

? ? public DiyStyleTextViewsetUnderlineText(boolean underlineText) {

this.underlineText = underlineText;

return this;

? ? }

//添加自定義顏色規(guī)則

? ? public DiyStyleTextViewaddColorRegex(String colorRegex, int color) {

setMovementMethod(LinkMovementMethod.getInstance());

? ? ? ? colorRegexList.add(colorRegex);

? ? ? ? colorList.add(color);

return this;

? ? }

//添加自定義圖片規(guī)則

? ? public DiyStyleTextViewaddImageRegex(String imageRegex, Bitmap bitmap) {

setMovementMethod(LinkMovementMethod.getInstance());

? ? ? ? imageRegexList.add(imageRegex);

? ? ? ? bitmapList.add(bitmap);

return this;

? ? }

//覆蓋父類,setText()生效

? ? @Override

? ? public void setText(CharSequence text, BufferType type) {

super.setText(setTextStyle(text, false), type);

? ? }

//覆蓋父類,append()生效

? ? @Override

? ? public void append(CharSequence text, int start, int end) {

super.append(setTextStyle(text, false), start, end);

? ? }

public void setDiyTextColor(CharSequence text, String regularExpression, int color, DiyTextClick mDiyTextClick) {

addColorRegex(regularExpression, color).setDiyTextClickListenner(mDiyTextClick).setTextStyle(text, true);

? ? }

public void setDiyTextColor(CharSequence text, String regularExpression, int color) {

setDiyTextColor(text, regularExpression, color, null);

? ? }

public void setDiyTextImage(CharSequence text, String regularExpression, Bitmap bitmap, DiyTextClick mDiyTextClick) {

addImageRegex(regularExpression, bitmap).setDiyTextClickListenner(mDiyTextClick).setTextStyle(text, true);

? ? }

public void setDiyTextImage(CharSequence text, String regularExpression, Bitmap bitmap) {

setDiyTextImage(text, regularExpression, bitmap, null);

? ? }

private ListindexArr =new ArrayList<>();

? ? private ListstrArr =new ArrayList<>();

? ? public CharSequencesetTextStyle(CharSequence text, boolean flag) {

if (TextUtils.isEmpty(text)) {

if (flag)super.setText(text);

? ? ? ? ? ? return text;

? ? ? ? }

SpannableStringBuilder styledText =new SpannableStringBuilder(text);

? ? ? ? if (colorRegexList !=null)

for (int j =0; j

String colorRegex =colorRegexList.get(j);

? ? ? ? ? ? ? ? int color =colorList.get(j);

? ? ? ? ? ? ? ? indexArr.clear();

? ? ? ? ? ? ? ? strArr.clear();

? ? ? ? ? ? ? ? //正則獲取符合的字符串

? ? ? ? ? ? ? ? Pattern p = Pattern.compile(colorRegex);

? ? ? ? ? ? ? ? Matcher m = p.matcher(text);

? ? ? ? ? ? ? ? while (m.find()) {

strArr.add(m.group());

? ? ? ? ? ? ? ? ? ? indexArr.add(m.start());

? ? ? ? ? ? ? ? }

for (int i =0; i

int index =indexArr.get(i);

? ? ? ? ? ? ? ? ? ? String clickText =strArr.get(i);

? ? ? ? ? ? ? ? ? ? //替換文本 自定義風(fēng)格

? ? ? ? ? ? ? ? ? ? styledText.setSpan(

diyTextClickListenner ==null ?

new TextViewCharacterStyle(color) :

new TextViewClickSpan(clickText, color),

? ? ? ? ? ? ? ? ? ? ? ? ? ? index,

? ? ? ? ? ? ? ? ? ? ? ? ? ? index + clickText.length(),

? ? ? ? ? ? ? ? ? ? ? ? ? ? Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

? ? ? ? ? ? ? ? }

}

if (imageRegexList !=null)

for (int j =0; j

String imageRegex =imageRegexList.get(j);

? ? ? ? ? ? ? ? Bitmap bitmap =bitmapList.get(j);

? ? ? ? ? ? ? ? indexArr.clear();

? ? ? ? ? ? ? ? strArr.clear();

? ? ? ? ? ? ? ? Pattern p = Pattern.compile(imageRegex);

? ? ? ? ? ? ? ? Matcher m = p.matcher(text);

? ? ? ? ? ? ? ? while (m.find()) {

strArr.add(m.group());

? ? ? ? ? ? ? ? ? ? indexArr.add(m.start());

? ? ? ? ? ? ? ? }

for (int i =0; i

int index =indexArr.get(i);

? ? ? ? ? ? ? ? ? ? String clickText =strArr.get(i);

? ? ? ? ? ? ? ? ? ? styledText.setSpan(

new ImageSpan(bitmap),

? ? ? ? ? ? ? ? ? ? ? ? ? ? index,

? ? ? ? ? ? ? ? ? ? ? ? ? ? index + clickText.length(),

? ? ? ? ? ? ? ? ? ? ? ? ? ? Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

? ? ? ? ? ? ? ? ? ? styledText.setSpan(

diyTextClickListenner ==null ?

null :

new TextViewClickSpan(clickText, 0),

? ? ? ? ? ? ? ? ? ? ? ? ? ? index,

? ? ? ? ? ? ? ? ? ? ? ? ? ? index + clickText.length(),

? ? ? ? ? ? ? ? ? ? ? ? ? ? Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

? ? ? ? ? ? ? ? }

}

if (flag)super.setText(styledText);

? ? ? ? return styledText;

? ? }

//設(shè)置顏色 點擊監(jiān)聽的Span

? ? private class TextViewClickSpanextends ClickableSpan {

private StringclickText;

? ? ? ? private int color;

? ? ? ? TextViewClickSpan(String clickText) {

this.clickText = clickText;

? ? ? ? }

TextViewClickSpan(String clickText, int color) {

this.clickText = clickText;

? ? ? ? ? ? this.color = color;

? ? ? ? }

@Override

? ? ? ? public void updateDrawState(TextPaint ds) {

if (color !=0) ds.setColor(color);

? ? ? ? ? ? ds.setUnderlineText(underlineText); //下劃線

? ? ? ? }

@Override

? ? ? ? public void onClick(View widget) {//點擊事件

? ? ? ? ? ? if (diyTextClickListenner !=null)

diyTextClickListenner.diyTextClick(clickText);

? ? ? ? }

}

//只有設(shè)置顏色Span

? ? private class TextViewCharacterStyleextends CharacterStyle {

private int color;

? ? ? ? TextViewCharacterStyle(int color) {

this.color = color;

? ? ? ? }

@Override

? ? ? ? public void updateDrawState(TextPaint ds) {

if (color !=0) ds.setColor(color);

? ? ? ? ? ? ds.setUnderlineText(underlineText); //下劃線

? ? ? ? }

}

private DiyTextClickdiyTextClickListenner;

? ? public interface DiyTextClick {

void diyTextClick(String s);

? ? }

//點擊監(jiān)聽-全局

? ? public DiyStyleTextViewsetDiyTextClickListenner(DiyTextClick mDiyTextClick) {

this.diyTextClickListenner = mDiyTextClick;

? ? ? ? setClickable(true);

return this;

? ? }

}


使用方法:

diytextview.addColorRegex("[\\d]+", 0xff2CA298)

? ? ? ? ? ? ? ? .addColorRegex("[a-zA-Z]+", 0xffff6666)

? ? ? ? ? ? ? ? .addImageRegex("\\[d_aini\\]", BitmapFactory.decodeResource(getResources(), R.drawable.d_aini))

? ? ? ? ? ? ? ? .addImageRegex("\\[d_doge\\]", BitmapFactory.decodeResource(getResources(), R.drawable.d_doge));

? diytextview.setText("[d_doge]熱愛工作Work2018[d_aini]");

? diytextview.append("\n[d_doge]熱愛生活Life2018[d_aini]");

? diytextview.append("\n[d_doge]熱愛學(xué)習(xí)Learn1314[d_aini]");

? diytextview.append("\n[d_doge]熱愛學(xué)習(xí)Learn1314[d_aini]");

? diytextview.append("\n[d_doge]熱愛學(xué)習(xí)Learn1314[d_aini]");

? diytextview.append("\n[d_doge]熱愛學(xué)習(xí)Learn1314[d_aini]");

? diytextview.append("\n[d_doge]熱愛學(xué)習(xí)Learn1314[d_aini]");

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市雾袱,隨后出現(xiàn)的幾起案子恤筛,更是在濱河造成了極大的恐慌,老刑警劉巖芹橡,帶你破解...
    沈念sama閱讀 221,695評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件毒坛,死亡現(xiàn)場離奇詭異,居然都是意外死亡林说,警方通過查閱死者的電腦和手機煎殷,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,569評論 3 399
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來腿箩,“玉大人豪直,你說我怎么就攤上這事《让兀” “怎么了顶伞?”我有些...
    開封第一講書人閱讀 168,130評論 0 360
  • 文/不壞的土叔 我叫張陵饵撑,是天一觀的道長剑梳。 經(jīng)常有香客問我,道長滑潘,這世上最難降的妖魔是什么垢乙? 我笑而不...
    開封第一講書人閱讀 59,648評論 1 297
  • 正文 為了忘掉前任,我火速辦了婚禮语卤,結(jié)果婚禮上追逮,老公的妹妹穿的比我還像新娘。我一直安慰自己粹舵,他們只是感情好钮孵,可當(dāng)我...
    茶點故事閱讀 68,655評論 6 397
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著眼滤,像睡著了一般巴席。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上诅需,一...
    開封第一講書人閱讀 52,268評論 1 309
  • 那天漾唉,我揣著相機與錄音,去河邊找鬼堰塌。 笑死赵刑,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的场刑。 我是一名探鬼主播般此,決...
    沈念sama閱讀 40,835評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了铐懊?” 一聲冷哼從身側(cè)響起屎勘,我...
    開封第一講書人閱讀 39,740評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎居扒,沒想到半個月后概漱,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 46,286評論 1 318
  • 正文 獨居荒郊野嶺守林人離奇死亡喜喂,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,375評論 3 340
  • 正文 我和宋清朗相戀三年瓤摧,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片玉吁。...
    茶點故事閱讀 40,505評論 1 352
  • 序言:一個原本活蹦亂跳的男人離奇死亡照弥,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出进副,到底是詐尸還是另有隱情这揣,我是刑警寧澤,帶...
    沈念sama閱讀 36,185評論 5 350
  • 正文 年R本政府宣布影斑,位于F島的核電站给赞,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏矫户。R本人自食惡果不足惜片迅,卻給世界環(huán)境...
    茶點故事閱讀 41,873評論 3 333
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望皆辽。 院中可真熱鬧柑蛇,春花似錦、人聲如沸驱闷。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,357評論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽空另。三九已至盆耽,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間痹换,已是汗流浹背征字。 一陣腳步聲響...
    開封第一講書人閱讀 33,466評論 1 272
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留娇豫,地道東北人匙姜。 一個月前我還...
    沈念sama閱讀 48,921評論 3 376
  • 正文 我出身青樓,卻偏偏與公主長得像冯痢,于是被迫代替她去往敵國和親氮昧。 傳聞我的和親對象是個殘疾皇子框杜,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,515評論 2 359

推薦閱讀更多精彩內(nèi)容