ReplacementSpan 在棉花糖中不起作用 [英] ReplacementSpan not working in Marshmallow

查看:39
本文介绍了ReplacementSpan 在棉花糖中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 ReplacementSpan 为 TextView 添加了文本描边.它在 Marshmallow 下的 Android Sdk 中完美运行.但是,在棉花糖中,它显示空白文本.当我将 ForegroundSpan 添加到 Top TextView 然后将我的自定义跨度设置为下面的视图时,它会起作用.我不明白我有什么遗漏.
下面是我的 TextSpannable 类代码 -

I'v added Text Stroke for TextView using ReplacementSpan. It's working perfectly in Android Sdk below Marshmallow. But, In marshmallow it showing blank text. When I'v added ForegroundSpan to Top TextView and then set my custom span to below views it work's. I don't understand is there anything I'm missing.
Below is my TextSpannable Class code -

public class TextSpannable extends ReplacementSpan {

    private final Paint paintFill = new Paint(Paint.ANTI_ALIAS_FLAG);
    private final Paint paintStroke = new Paint(Paint.ANTI_ALIAS_FLAG);
    private final Path path = new Path();
    private int width;

    public TextSpannable(int strokeWidth) {
       paintFill.setColor(Color.WHITE);
       paintStroke.setStyle(Paint.Style.STROKE);
       paintStroke.setStrokeWidth(6);
    }

    public void setPathEffect(PathEffect effect) {
       paintStroke.setPathEffect(effect);
    }

    @Override
    public int getSize(Paint paint, CharSequence text, int start, 
       int end,   Paint.FontMetricsInt fm) {
       this.paintFill.setColor(Color.WHITE);
       this.paintStroke.setColor(Color.parseColor("#402002"));
       width = (int) (paint.measureText(text, start, end) +  
       this.paintStroke.getStrokeWidth() + paintFill.getTextSize());
       return width;
    }

    @Override
    public void draw(Canvas canvas, CharSequence text, int start, int end, 
         float x, int top, int y,int bottom, Paint paint) {
        path.reset();
        paint.getTextPath(text.toString(), start, end, x, y, path);
        path.close();

        canvas.translate(this.paintStroke.getStrokeWidth() / 2, 0);
        canvas.drawPath(path,this.paintStroke);
        canvas.drawPath(path, this.paintFill);
        canvas.translate(-this.paintStroke.getStrokeWidth() / 2, 0);
        //canvas.drawText(this.text,start,end,x,y,paintFill);
        //canvas.drawText(this.text,start,end,x,y,paintStroke);
     }
}

这就是我将它设置为 TextView 的方式 -

This is how I set it to TextView -

    spannableString7 = 
    new SpannableStringBuilder(getString(R.string.string1));
    ForegroundColorSpan fcs  =new ForegroundColorSpan(Color.TRANSPARENT);
    spannableString7.setSpan(fcs,0, getString(R.string.string1).length(),Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); 
    textSpam.setText(spannableString7, TextView.BufferType.SPANNABLE);    

   //when I comment above block, text becomes blank in Textview

    spannableString1 = 
    new SpannableStringBuilder(getString(R.string.string1));
    spannableString1.setSpan(textSpannable,0,getString(R.string.string1).length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    textView.setText(spannableString1, TextView.BufferType.SPANNABLE);

推荐答案

我想出了如何让它发挥作用.您必须实现 LineHeightSpan 因为如果您单独使用 ReplacementSpan 那么我想 TextView 不知道您的文本的高度是多少?只需实现 LineHeightSpan 即可解决问题.

I figured how to make it work. You must implement LineHeightSpan because if you use the ReplacementSpan by itself then I suppose the TextView doesn't know what height your text has ? Just implementing LineHeightSpan will fix the problem.

public class CoolBackgroundColorSpan extends ReplacementSpan implements LineHeightSpan {

    @Override
    public int getSize(@NonNull Paint paint, CharSequence text, int start, int end, Paint.FontMetricsInt fm) {
        // Your code here
    }

    @Override
    public void draw(@NonNull Canvas canvas, CharSequence text, int start, int end, float x, int top, int y, int bottom, @NonNull Paint paint) {
        // Your code here
    }

    @Override
    public void chooseHeight(CharSequence charSequence, int i, int i1, int i2, int i3, Paint.FontMetricsInt fontMetricsInt) {
        // This method is needed because the class implements LineHeightSpan.
        // We don't modify the fontMetricsInt because we don't want to modify the line height.
        // The line height will have the height of the text
    }

}

如果您还想更改行高,请参阅此答案 https://stackoverflow.com/a/39044426/1502079.

If you'd like also to change the line height then see this answer https://stackoverflow.com/a/39044426/1502079.

这篇关于ReplacementSpan 在棉花糖中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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