Android的Spannablecontent带有圆角 [英] Android Spannablecontent With Rounded Corners

查看:1353
本文介绍了Android的Spannablecontent带有圆角的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想改变我的字符串使用Spannable字符串,使与一些中间徽章。我可以突出显示相应的字母/数字通过设置BackGroundColorSpan,但需要别人帮助了一点prettier。我希望有圆角与填充围绕整个造型一点点。

这篇文章是非常接近我想要做的事:<一href="http://stackoverflow.com/questions/19292838/android-spannablestring-set-background-behind-part-of-text">Android SpannableString设置文本背后的一部分背景

我真的需要保留的资源作为一个TextView因为它与我的应用程序交互的方式。

任何想法如何利用ReplacementSpan我的特殊情况?

下面是我的code片断:

 如果(menu​​Item.getMenuItemType()== SlidingMenuItem.MenuItemType.NOTIFICATIONS){
                myMenuRow.setTypeface(NULL,Typeface.NORMAL);
                myMenuRow.setTextColor(。的getContext()getResources()的getColor(R.color.BLACK));
                myMenuRow.setActivated(假);
                SpannableString spannablecontent =新SpannableString(myMenuRow.getText());
                spannablecontent.setSpan(新BackgroundColorSpan(Color.argb(150,0,0,0)),18,myMenuRow.length(),Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                myMenuRow.setText(spannablecontent);
 

解决方案

阅读开始有点帮助,转换器的C#后,我想出了这一点。我仍然有一些调整的事情,但如果有人还找了类似的回答。

 公共类RoundedBackgroundSpan扩展ReplacementSpan
{

    @覆盖
    公众诠释的getSize(油漆涂料,CharSequence的文字,诠释开始,诠释年底,Paint.FontMetricsInt FM){
        返回0;
    }

    @覆盖
    公共无效画(油画画布,CharSequence的文字,诠释开始,诠释年底,浮法X,INT顶部,INT Y,INT底部,油漆涂料)
    {
        RectF RECT =新RectF(X,顶部,X + text.length(),底部);
        paint.setColor(Color.CYAN);
        canvas.drawRoundRect(矩形,20,20,油漆);
        paint.setColor(Color.WHITE);
        canvas.drawText(文本,开始,结束,X,Y,油漆);
    }
}
 

I am trying to change my string to make a badge with a number in the middle by using Spannable String. I can highlight the appropriate letter/number by setting the BackGroundColorSpan, but need help making it a little prettier. I was hoping to have rounded corners with a little bit of padding around the entire shape.

This article is really close to what I'm trying to do: Android SpannableString set background behind part of text

I really need to keep the resource as a TextView due to the way it interacts with my application.

Any ideas how to utilize ReplacementSpan for my particular situation?

Here is my code snippet:

            if (menuItem.getMenuItemType() == SlidingMenuItem.MenuItemType.NOTIFICATIONS) {
                myMenuRow.setTypeface(null, Typeface.NORMAL);
                myMenuRow.setTextColor(getContext().getResources().getColor(R.color.BLACK));
                myMenuRow.setActivated(false);
                SpannableString spannablecontent = new SpannableString(myMenuRow.getText());
                spannablecontent.setSpan(new BackgroundColorSpan(Color.argb(150,0,0,0)), 18, myMenuRow.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                myMenuRow.setText(spannablecontent);

解决方案

After reading getting a little help with a converter for C#, I came up with this. I still have some tweaking to do, but if anyone is also looking for a similar answer.

public class RoundedBackgroundSpan extends ReplacementSpan
{

    @Override
    public int getSize(Paint paint, CharSequence text, int start, int end, Paint.FontMetricsInt fm) {
        return 0;
    }

    @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 + text.length(), bottom);
        paint.setColor(Color.CYAN);
        canvas.drawRoundRect(rect, 20, 20, paint);
        paint.setColor(Color.WHITE);
        canvas.drawText(text, start, end, x, y, paint);
    }
}

这篇关于Android的Spannablecontent带有圆角的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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