android textview 设置个别字体颜色
使用Spannable这个接口,他可以把一个对象标记为可链接,别离的,即可对字体样式进行动态的更改,用法如下:
TextView nameView=findViewById(R.id.test);
String str = “test”;
nameView.setText(str,TextView.BufferType.SPANNABLE);
Spannable sp = (Spannable) nameView.getText();
//sp.setSpan(new ForegroundColorSpan(Color.RED), 0 ,1,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); //这是直接将字体设置为红色。
sp.setSpan(new BackgroundColorSpan(Color.RED), 0 ,1,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); //这是将字体背景设置为红色。

浙公网安备 33010602011771号