Android SpannableString在文本的后面设置背景

我想创build一个类似于这个图像上看到的东西: 在这里输入图像说明

我设法用SpannableStringBuilder创buildevertyhing,除了橙色的圆angular矩形。 我可以使用BackgroundColorSpan将背景设置为该颜色,但我无法find使其圆整的方法。 任何想法我怎么能做到这一点?

提前致谢!

编辑:我使用Xamarin.Android,但这里是我的代码:

stringBuilder.SetSpan(new BackgroundColorSpan(Application.Context.Resources.GetColor(Resource.Color.orangeColor)), stringBuilder.Length() - length, stringBuilder.Length(), SpanTypes.ExclusiveExclusive); 

Solutions Collecting From Web of "Android SpannableString在文本的后面设置背景"

如果有人对罗斯福的代码示例有困难(我确定,也许是因为它是Xamarin.Android?),下面是一个更基本的Android Java版本的翻译:

 public class RoundedBackgroundSpan extends ReplacementSpan { private static int CORNER_RADIUS = 8; private int backgroundColor = 0; private int textColor = 0; public RoundedBackgroundSpan(Context context) { super(); backgroundColor = context.getResources().getColor(R.color.gray); textColor = context.getResources().getColor(R.color.white); } @Override public void draw(Canvas canvas, CharSequence text, int start, int end, float x, int top, int y, int bottom, Paint paint) { RectF rect = new RectF(x, top, x + measureText(paint, text, start, end), bottom); paint.setColor(backgroundColor); canvas.drawRoundRect(rect, CORNER_RADIUS, CORNER_RADIUS, paint); paint.setColor(textColor); canvas.drawText(text, start, end, x, y, paint); } @Override public int getSize(Paint paint, CharSequence text, int start, int end, Paint.FontMetricsInt fm) { return Math.round(paint.measureText(text, start, end)); } private float measureText(Paint paint, CharSequence text, int start, int end) { return paint.measureText(text, start, end); } } 

而对于用法,下面的代码段是从一个活动中获取的,并且基本上在每个标签string周围放置了一个很好的圆angular背景,每个标签之间都有一个空间缓冲区。 请注意,注释掉的线只是放在一个背景颜色,这不会产生一个好看的…

 SpannableStringBuilder stringBuilder = new SpannableStringBuilder(); String between = ""; for (String tag : eventListing.getTags()) { stringBuilder.append(between); if (between.length() == 0) between = " "; String thisTag = " "+tag+" "; stringBuilder.append(thisTag); stringBuilder.setSpan(new RoundedBackgroundSpan(this), stringBuilder.length() - thisTag.length(), stringBuilder.length() - thisTag.length() + thisTag.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); //stringBuilder.setSpan(new BackgroundColorSpan(getResources().getColor(R.color.gray)), stringBuilder.length() - thisTag.length(), stringBuilder.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); } TextView tv = new TextView(this); tv.setText(stringBuilder); 

基于pskink的build议,我设法解决了我的问题。 这是我的class级:

 public class RoundedBackgroundSpan : ReplacementSpan { public override void Draw(Canvas canvas, ICharSequence text, int start, int end, float x, int top, int y, int bottom, Paint paint) { var rect = new RectF(x, top, x + MeasureText(paint, text, start, end), bottom); paint.Color = Application.Context.Resources.GetColor(Resource.Color.nextTimeBackgroundColor); canvas.DrawRoundRect(rect, Application.Context.Resources.GetDimensionPixelSize(Resource.Dimension.localRouteDetailsRoundRectValue), Application.Context.Resources.GetDimensionPixelSize(Resource.Dimension.localRouteDetailsRoundRectValue), paint); paint.Color = Application.Context.Resources.GetColor(Resource.Color.nextTimeTextColor); canvas.DrawText(text, start, end, x, y, paint); } public override int GetSize(Paint paint, ICharSequence text, int start, int end, Paint.FontMetricsInt fm) { return Math.Round(MeasureText(paint, text, start, end)); } private float MeasureText(Paint paint, ICharSequence text, int start, int end) { return paint.MeasureText(text, start, end); } } 

用法示例:

 var stringBuilder = new SpannableStringBuilder(); var stringToAppend = "hello world"; stringBuilder.Append(stringToAppend); stringBuilder.SetSpan(new RoundedBackgroundSpan(), stringBuilder.Length() - stringToAppend.Length, stringBuilder.Length(), SpanTypes.ExclusiveExclusive); 

只是一个字:ReplacementSpan