Android的SpannableString设置背景的背后文本的一部分 [英] Android SpannableString set background behind part of text

查看:2073
本文介绍了Android的SpannableString设置背景的背后文本的一部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建类似的东西因为看到这个图片:

我设法创建SpannableStringBuilder evertyhing,除了橙色的圆角矩形。我可以设置背景与BackgroundColorSpan的颜色,但我不能找到一种方法,使四舍五入。任何想法我怎么能做到这一点?

在此先感谢!

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

  stringBuilder.SetSpan(新BackgroundColorSpan(Application.Context.Resources.GetColor(Resource.Color.orangeColor)),stringBuilder.Length() - 长度,stringBuilder.Length(),SpanTypes .ExclusiveExclusive);
 

解决方案

我设法解决我的问题的基础上,pskink的建议。 这是我的类:

 公共类RoundedBackgroundSpan:ReplacementSpan
{
    公众覆盖无效抽奖(画布油画,ICharSequence文字,诠释开始,诠释年底,浮法X,INT顶部,INT Y,INT底部,油漆涂料)
    {
        VAR RECT =新RectF(X,顶部,X + MeasureText(涂料,文本,开始,结束),底部);
        paint.Color = Application.Context.Resources.GetColor(Resource.Color.nextTimeBackgroundColor);
        canvas.DrawRoundRect(矩形, Application.Context.Resources.GetDimensionPixelSize(Resource.Dimension.localRouteDetailsRoundRectValue), Application.Context.Resources.GetDimensionPixelSize(Resource.Dimension.localRouteDetailsRoundRectValue),涂料);
        paint.Color = Application.Context.Resources.GetColor(Resource.Color.nextTimeTextColor);
        canvas.DrawText(文本,开始,结束,X,Y,油漆);
    }

    公众覆盖INT的getSize(油漆涂料,ICharSequence文字,诠释开始,诠释年底,Paint.FontMetricsInt FM)
    {
        返回Math.Round(MeasureText(油漆,文本,开始,结束));
    }

    私人浮动MeasureText(油漆涂料,ICharSequence文字,诠释开始,诠释完)
    {
        返回paint.MeasureText(文本,开始,结束);
    }
}
 

实例:

  VAR的StringBuilder =新SpannableStringBuilder();
VAR stringToAppend =世界你好;
stringBuilder.Append(stringToAppend);
stringBuilder.SetSpan(新RoundedBackgroundSpan(),stringBuilder.Length() -  stringToAppend.Length,stringBuilder.Length(),SpanTypes.ExclusiveExclusive);
 

I would like to create something similar as seen on this image:

I managed to create evertyhing with SpannableStringBuilder, except the orange rounded rectangle. I can set the background to that color with BackgroundColorSpan, but I can't find a way to make it rounded. Any ideas how can I achieve this?

Thanks in advance!

EDIT: I'm using Xamarin.Android, but here is my code:

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

解决方案

I managed to solve my problem, based on pskink's suggestion. Here is my 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);
    }
}

Example usage:

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

这篇关于Android的SpannableString设置背景的背后文本的一部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆