我想创建类似此图片所示的内容:
我设法用SpannableStringBuilder创建了Evertyhing,除了橙色的圆角矩形。我可以使用BackgroundColorSpan将背景设置为该颜色,但是我找不到使它变圆的方法。有什么想法可以实现吗?
提前致谢!
编辑:我正在使用Xamarin.Android,但这是我的代码:
stringBuilder.SetSpan(new BackgroundColorSpan(Application.Context.Resources.GetColor(Resource.Color.orangeColor)), stringBuilder.Length() - length, stringBuilder.Length(), SpanTypes.ExclusiveExclusive);
根据pskink的建议,我设法解决了我的问题。这是我的课:
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);
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句