2016-12-06 5 views
1

Ich habe Text Stroke für TextView mit ReplacementSpan hinzugefügt. Es funktioniert perfekt in Android Sdk unter Marshmallow. Aber, in Marshmallow zeigt es leeren Text. Wenn ich ForegroundSpan zu Top TextView hinzugefügt habe und meinen benutzerdefinierten Bereich auf unter Ansichten eingestellt habe, funktioniert es. Ich verstehe nicht, ob ich etwas vermisse.
Unten ist mein TextSpannable Klasse Code -ReplacementSpan funktioniert nicht in Marshmallow

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

Dies ist, wie ich es Textview-Set -

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

Antwort

0

Ich dachte, wie es funktioniert. Sie müssen LineHeightSpan implementieren, denn wenn Sie die ReplacementSpan von selbst verwenden, dann nehme ich an, TextView weiß nicht, welche Höhe Ihr Text hat? Nur die Implementierung LineHeightSpan wird das Problem beheben. sehen Sie diese Antwort https://stackoverflow.com/a/39044426/1502079

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 
    } 

} 

Wenn auch Sie möchten, dass dann die Zeilenhöhe ändern.

+0

funktioniert nicht für mich –

+0

@Karandeep Atwal Vielleicht zeichnest du nichts. – vovahost

+0

Ich habe die Breite von editText festgelegt und wenn ich tippe, wird Text nicht automatisch zur nächsten Zeile übertragen. –

Verwandte Themen