2016-12-06 121 views
1

我使用ReplacementSpan为TextView添加了Text Stroke。它在Marshmallow下的Android Sdk中完美运行。但是,在棉花糖它显示空白文本。当我将ForegroundSpan添加到顶部TextView,然后将我的自定义跨度设置为低于视图的工作。我不明白有什么我失踪。
下面是我TextSpannable类代码 -ReplacementSpan不能在棉花糖中工作

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 -

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); 

回答

0

我想出如何使它发挥作用。 你必须实现LineHeightSpan,因为如果你自己使用ReplacementSpan那么我想TextView不知道你的文本有多高? 只需执行LineHeightSpan即可解决问题。

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

+0

不适合我 –

+0

@Karandeep Atwal也许你没有画任何东西。 – vovahost

+0

我有固定的editText宽度,当我输入时,文本不会自动转移到下一行。 –