1、使用Spannable这个接口,他可以把一个对象标记为可链接,别离的,即可对字体样式进行动态的更改,此方法是改变的背景颜色,用法如下:
TextView nameView=(TextView)findViewById(R.id.tv);
String str="我的一个android测试程序";
nameView.setText(str,TextView.BufferType.SPANNABLE);
Spannable sp = (Spannable) nameView.getText();
sp.setSpan(new BackgroundColorSpan(Color.BLUE), 4 ,11, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
2、 使用Spannable改变字体颜色:
TextView tv=(TextView)findViewById(R.id.tv);
String str="我的一个android测试程序";
SpannableStringBuilder stryle = new SpannableStringBuilder(str);
stryle.setSpan(new ForegroundColorSpan(Color.BLUE), 0, 4, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
stryle.setSpan(new ForegroundColorSpan(Color.RED), 4, 11, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
stryle.setSpan(new ForegroundColorSpan(Color.WHITE), 11, 15, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
tv.setText(stryle);
3、使用html,如:
import android.app.Activity;
import android.os.Bundle;
import android.text.Html;
import android.widget.TextView;
public class helloActivity extends Activity
{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView tv=(TextView)findViewById(R.id.tv);
tv.setText(Html.fromHtml("你好!我的<font color=blue>ANDROID</font>之旅!"));
}
}