如何使spannable柱之间的空间机器人? [英] How to make space between spannable string in Android?

查看:102
本文介绍了如何使spannable柱之间的空间机器人?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

code:

 私人无效setSpans(编辑S,@ColorInt INT的backgroundColor){

    BackgroundColorSpan []跨距= s.getSpans(0,s.length(),BackgroundColorSpan.class);
    的String []的话;
    如果(s.toString()。的endsWith()){
        也就是说=(s.toString()+X)分裂(\\ S)。
    } 其他 {
        也就是说= s.toString()分裂(\\ S)。
    }
    INT completedWordsCount = words.length  -  1;
    如果(spans.length!= completedWordsCount){
        对于(BackgroundColorSpan跨度:跨度){
            s.removeSpan(跨度);
        }

        INT CURRENTINDEX = 0;
        的for(int i = 0; I< words.length-1;我++){
            s.setSpan(新CustomDrawble(Color.GRAY,Color.WHITE),CURRENTINDEX,CURRENTINDEX +字[I] .length(),Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            CURRENTINDEX + =字[I] .length()+ 1;
        }
    }
 

以上的功能是用于创建有用的 spannable字符串边框添加到它。我打电话下面的类:

 公共类CustomDrawble扩展ReplacementSpan {

私人诠释mBackgroundColor;
私人诠释mForegroundColor;

公共CustomDrawble(INT的backgroundColor,诠释foregroundColor){
    this.mBackgroundColor =的backgroundColor;
    this.mForegroundColor = foregroundColor;
}

@覆盖
公众诠释的getSize(油漆涂料,CharSequence的文字,诠释开始,诠释年底,Paint.FontMetricsInt FM){
    返回Math.round(measureText(油漆,文本,开始,结束));

}

@覆盖
公共无效画(油画画布,CharSequence的文字,诠释开始,诠释年底,浮法X,INT顶部,INT Y,INT底部,油漆涂料){
    浮垫= 4F;

    RectF RECT =新RectF(X,顶部+ 3,X + measureText(涂料,文本,开始,结束)+ 10,底+ 10);
    paint.setColor(mBackgroundColor);
    canvas.drawRoundRect(矩形,10,10,油漆);
    paint.setColor(mForegroundColor);
    canvas.drawText(文本,开始,结束,X,Y,油漆);

}

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

结果我从上面得到的是:

输入图像的描述在这里

与上面的code的问题是:

  • 如何两个 spannable 字符串之间添加空间?
  • 我怎么可以增加空间的矩形,矩形的结束类似的字符串第一个字符的起点。我试图通过这个code添加空间:

      RectF RECT =新RectF(X + 10,前+ 3,X + measureText(涂料,文本,开始,结束)+ 10,底+ 10);
     

但它给了我这个扭曲的结果是:

输入图像的描述在这里

如果我没看错的话上述code中的问题是没有两个 spannable 字符串之间有足够的空间。

我怎样才能使其正确吗?

修改-1

用于本 RectF RECT =新RectF(X - 5,顶+ 3,X + measureText(涂料,文本,开始,结束)+5,底+ 10);

输入图像的描述在这里

看第一个它是略高于起点切割。但是,如果我用上面的配置我没有足够的空间在两个字符串。这是主要的问题。

解决方案

从年底,除去多余的像素的矩形将解决其跨度之间没有空格

所以删除 + 10 从参数右

是这样的:

  RectF RECT =新RectF(X,顶部+ 3,X + measureText(涂料,文本,开始,结束),底+ 10);
 

也是要开始添加空间放一些切缘阴性的起点 像这样的:

  RectF RECT =新RectF(X  -  5,顶+ 3,X + measureText(涂料,文本,开始,结束),底+ 10);
 

下面的图片将解释这一点视觉上:

挖

Code:

private void setSpans(Editable s, @ColorInt int backgroundColor) {

    BackgroundColorSpan[] spans = s.getSpans(0, s.length(), BackgroundColorSpan.class);
    String[] words;
    if (s.toString().endsWith(" ")) {
        words = (s.toString() + "X").split("\\s");
    } else {
        words = s.toString().split("\\s");
    }
    int completedWordsCount = words.length - 1;
    if (spans.length != completedWordsCount) {
        for (BackgroundColorSpan span : spans) {
            s.removeSpan(span);
        }

        int currentIndex = 0;
        for (int i = 0; i < words.length-1; i++) {
            s.setSpan(new CustomDrawble(Color.GRAY, Color.WHITE), currentIndex, currentIndex + words[i].length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            currentIndex += words[i].length() + 1;
        }
    }

Above function is useful for creating spannable String and to add Border to it. I am calling following class:

public class CustomDrawble extends ReplacementSpan {

private int mBackgroundColor;
private int mForegroundColor;

public CustomDrawble(int backgroundColor, int foregroundColor) {
    this.mBackgroundColor = backgroundColor;
    this.mForegroundColor = foregroundColor;
}

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

}

@Override
public void draw(Canvas canvas, CharSequence text, int start, int end, float x, int top, int y, int bottom, Paint paint) {
    float padding = 4f;

    RectF rect = new RectF(x, top + 3, x + measureText(paint, text, start, end) + 10, bottom + 10);
    paint.setColor(mBackgroundColor);
    canvas.drawRoundRect(rect, 10,10,paint);
    paint.setColor(mForegroundColor);
    canvas.drawText(text, start, end, x, y, paint);

}

private float measureText(Paint paint, CharSequence text, int start, int end) {
    return paint.measureText(text, start, end);
}
}

The result I am getting from above is:

The problem with the above code is:

  • How to add space between two spannable strings?
  • How can I add space at the starting of rectangle and first character of a string similar to end of rectangle. I tried to add the space by this code :

    RectF rect = new RectF(x+10, top + 3, x + measureText(paint, text, start, end) + 10, bottom + 10);
    

but it gave me this distorted result:

If I am not wrong then the problem with the above code is there are not enough spaces between two spannable strings.

How can I make it correct?

Edit-1

Used this RectF rect = new RectF(x - 5, top + 3, x + measureText(paint, text, start, end)+5, bottom + 10);

Look at the first one it's slightly cutting from starting point. But If I used the above configuration I don't have enough space in two strings. This is the main Issue.

解决方案

Removing extra pixels from end of your rect will solve the problem of having no space between spans

so remove + 10 from param for right

like this:

RectF rect = new RectF(x, top + 3, x + measureText(paint, text, start, end), bottom + 10);

also to add space at starting put some negative margin at starting like this:

 RectF rect = new RectF(x - 5, top + 3, x + measureText(paint, text, start, end), bottom + 10);

following image will explain this visually:

这篇关于如何使spannable柱之间的空间机器人?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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