SpannableString
如果您只想设置文本样式,您将需要使用
这是我在一些应用程序中使用的 util 函数来设置文本的颜色,您可以通过将其更改为BackgroundColorSpan
如果您想使用可绘制的(带有阴影的圆形绿色形状)做与图片中完全相同的事情,您可以使用DynamicDrawableSpan
.
见这里:http: //developer.android.com/reference/android/text/style/DynamicDrawableSpan.html
/**
* Taken with permission from PocketPermissions, this method is licensed under Apache 2.0
* Sets two colors in a single TextView or Button.
*/
public static void setTwoColorsTextStrings( String str1, String str2, TextView tv, Context c, int color1, int color2 )
{
SpannableString spnstr1 = new SpannableString( str1 );
SpannableString spnstr2 = new SpannableString( str2 );
int start = 0;
int len = str1.length();
int start2 = 0;
int len2 = str2.length();
spnstr1.setSpan( new ForegroundColorSpan( color1 ), start, len, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE );
spnstr2.setSpan( new ForegroundColorSpan( color2 ), start2, len2, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE );
SpannableString fullString = SpannableString.valueOf( TextUtils.concat( spnstr1, spnstr2 ) );
tv.setText( fullString, BufferType.SPANNABLE );
}