android中TextView分段显示不同颜色,字体,时间
例子1:
設(shè)置部分文字背景高亮顯示:?
?
同時設(shè)置文字和背景高亮顯示:?
參數(shù)說明:?
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE?
API里面解釋:Spans of type SPAN_EXCLUSIVE_EXCLUSIVE do not expand to include text inserted at either their starting or ending point. They can never have a length of 0 and are automatically removed from the buffer if all the text they cover is removed.?
即在原文本頭或尾追加新文本的樣式不受原文本樣式影響,原文本高亮,新追加文本不高亮。
Spannable.SPAN_EXCLUSIVE_INCLUSIVE?
API里面解釋:Non-0-length spans of type SPAN_INCLUSIVE_EXCLUSIVE expand to include text inserted at their ending point but not at their starting point. When 0-length, they behave like points.?
即在原文本尾追加新文本的樣式受原文本樣式影響,原來文本尾高亮,新追加文本也高亮
例子2:
功能:實現(xiàn)在一個TextView里顯示了一行字符串,需要這個串里字符顯示不同顏色.
代碼如下:
attrs.xml文件:
第二種方式:
?String newMessageInfo = "<font color='red'><b>" + 紅色內(nèi)容
??? ??? ??? ??? + "</b></font>TextView學(xué)習(xí)顯示不同顏色";
? mTextView.setTextView(Html.fromHtml(newMessageInfo));
?
例子3:
SpannableString ss = new SpannableString( "紅色打電話粗體刪除線綠色下劃線圖片:.");
? ?? ???ss.setSpan(new ForegroundColorSpan(Color.RED), 0, 2,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
? ?? ???ss.setSpan(new URLSpan("tel:4155551212"), 2, 5,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
? ?? ???ss.setSpan(new StyleSpan(Typeface.BOLD), 5, 7, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
? ?? ???ss.setSpan(new StrikethroughSpan(), 7, 10,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
? ?? ???ss.setSpan(new UnderlineSpan(), 10, 16,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
? ?? ???ss.setSpan(new ForegroundColorSpan(Color.GREEN), 10, 15, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
? ?? ???Drawable d = getResources().getDrawable(R.drawable.icon48x48_1);?
? ?? ???d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());?
? ?? ???ImageSpan span = new ImageSpan(d, ImageSpan.ALIGN_BASELINE);?
? ?? ???ss.setSpan(span, 18, 19, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);?
? ?? ???TextView t4 = (TextView) findViewById(R.id.text4);
? ?? ???t4.setText(ss);
? ?? ???
? ?? ???t4.setMovementMethod(LinkMovementMethod.getInstance());
? ? ? ??
例子4:
TextView是用來顯示文本的,有時需要給TextView中的個別字設(shè)置為超鏈接,或者設(shè)置個別字的顏色、字體等,那就需要用到Spannable對象,可以借助Spannable對象實現(xiàn)以上設(shè)置。?效果圖:?
Activity代碼:?
public?class?TextViewLinkActivity?extends?Activity?{??????? TextView?myTextView;??????? @Override????? public?void?onCreate(Bundle?savedInstanceState)?{??????????? super.onCreate(savedInstanceState);??????????? setContentView(R.layout.main);??????????? myTextView?=?(TextView)?this.findViewById(R.id.myTextView);?????????????????????? //創(chuàng)建一個?SpannableString對象?????????? SpannableString?sp?=?new?SpannableString("這句話中有百度超鏈接,有高亮顯示,這樣,或者這樣,還有斜體.");??????????? //設(shè)置超鏈接????????? sp.setSpan(new?URLSpan("http://www.baidu.com"),?5,?7,??????????????????? Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);?????????? //設(shè)置高亮樣式一????????? sp.setSpan(new?BackgroundColorSpan(Color.RED),?17?,19,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);??????????? //設(shè)置高亮樣式二??????????sp.setSpan(new?ForegroundColorSpan(Color.YELLOW),20,24,Spannable.SPAN_EXCLUSIVE_INCLUSIVE);????????????? //設(shè)置斜體?????????? sp.setSpan(new?StyleSpan(android.graphics.Typeface.BOLD_ITALIC),?27,?29,?Spannable.SPAN_EXCLUSIVE_INCLUSIVE);?????????? //SpannableString對象設(shè)置給TextView?????????? myTextView.setText(sp);?????????? //設(shè)置TextView可點擊???????? myTextView.setMovementMethod(LinkMovementMethod.getInstance());?????? }??? }
文章轉(zhuǎn)自:http://hi.baidu.com/simayilong80/blog/item/9ba5ebd958e53b3111df9bad.html
總結(jié)
以上是生活随笔為你收集整理的android中TextView分段显示不同颜色,字体,时间的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: android ViewPager动画的
- 下一篇: Android AsyncTask两种线