欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

android 更改TextView中任意位置字體大小和顏色的方法

 更新時間:2018年01月03日 15:11:23   作者:keyboy_rl  
下面小編就為大家分享一篇android 更改TextView中任意位置字體大小和顏色的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧

這里介紹兩種方法,一種是Spannable,一種是Html.fromHtml(通過html標(biāo)簽來改變),實際中看您使用哪種方便選擇使用即可

1.Html.fromHtml的使用

TextView textView = (TextView) findViewById(R.id.text);
String textSource = "修改TextView中部分文字的<font color='#ff0000'><big>大</big><small>小</small></font>和<font color='#00ff00'>顏色</font>,展示多彩效果!";
textView.setText(Html.fromHtml(textSource));

上面是沒有加html標(biāo)簽,下面是加了html標(biāo)簽的效果:

2.使用Spannable來實現(xiàn)

textView = (TextView) findViewById(R.id.textview); 
SpannableStringBuilder builder = new SpannableStringBuilder(textView.getText().toString()); 
//ForegroundColorSpan 為文字前景色,BackgroundColorSpan為文字背景色 
ForegroundColorSpan redSpan = new ForegroundColorSpan(Color.RED); 
ForegroundColorSpan whiteSpan = new ForegroundColorSpan(Color.WHITE); 
ForegroundColorSpan blueSpan = new ForegroundColorSpan(Color.BLUE); 
ForegroundColorSpan greenSpan = new ForegroundColorSpan(Color.GREEN); 
ForegroundColorSpan yellowSpan = new ForegroundColorSpan(Color.YELLOW); 
builder.setSpan(redSpan, 0, 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); 
builder.setSpan(whiteSpan, 1, 2, Spannable.SPAN_INCLUSIVE_INCLUSIVE); 
builder.setSpan(blueSpan, 2, 3, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); 
builder.setSpan(greenSpan, 3, 4, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); 
builder.setSpan(yellowSpan, 4,5, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); 
textView.setText(builder); 

使用Spannable效果如下圖:

是不是很簡單,但是效果強大,趕緊來實現(xiàn)吧?。?!

以上這篇android 更改TextView中任意位置字體大小和顏色的方法就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論