2014-04-08 10 views
8

Ich mag würde den Text wie unten zeigen ...Textview mit Hintergrundfarbe und Zeilenabstand

enter image description here

Meine Codierung ist die folgende:

SpannableString sText = new SpannableString(text); 
sText.setSpan(new BackgroundColorSpan(Color.YELLOW), 0, sText.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); 

holder.txtText.setLineSpacing(0, 1.5f); 
textView.setText(sText); 
+3

Ich bin auch mit dem gleichen Problem konfrontiert. Hast du irgendeine Lösung gefunden? – saberrider

Antwort

4

versuchen dies. Erstellen Sie ein benutzerdefiniertes TextView- und Override-Methoden-Draw (Canvas-Zeichenbereich).

public class BgColorTextView extends TextView { 
    public BgColorTextView(Context context) { 
     super(context); 
    } 

    public BgColorTextView(Context context, AttributeSet attrs) { 
     super(context, attrs); 
    } 

    public BgColorTextView(Context context, AttributeSet attrs, int defStyleAttr) { 
     super(context, attrs, defStyleAttr); 
    } 

    public BgColorTextView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { 
     super(context, attrs, defStyleAttr, defStyleRes); 
    } 

    @Override 
    public void draw(Canvas canvas) { 
     int lineCount = getLayout().getLineCount(); 
     Rect rect = new Rect(); 
     Paint paint = new Paint(); 
     paint.setColor(getResources().getColor(R.color.YOUR_CUSTOM_COLOR)); 
     for (int i = 0; i < lineCount; i++) { 
      rect.top = (getLayout().getLineTop(i)); 
      rect.left = (int) getLayout().getLineLeft(i); 
      rect.right = (int) getLayout().getLineRight(i); 
      rect.bottom = (int) (getLayout().getLineBottom(i) - ((i + 1 == lineCount) ? 0 : getLayout().getSpacingAdd())); 

      canvas.drawRect(rect, paint); 
     } 
     super.draw(canvas); 
    } 
} 
1

Es ist eine lange Zeit, aber ich werde die statische Methode unten setzen. Hoffentlich hilft es einem anderen.

public static void setTitleTextWithSpan(final TextView textView, int backgroundColor, String text, float lineSpacingMultiplier) { 
    class BackgroundColorSpanWithPaddingAndLineSpacing implements LineBackgroundSpan { 
     private float roundedCornerSize; 
     private int backgroundColor; 
     private int paddingSize; 
     private RectF rect; 

     private BackgroundColorSpanWithPaddingAndLineSpacing(int backgroundColor, int paddingSize, float roundedCornerSize) { 
      super(); 
      this.backgroundColor = backgroundColor; 
      this.paddingSize = paddingSize; 
      this.roundedCornerSize = roundedCornerSize; 
      this.rect = new RectF(); 
     } 

     @Override 
     public void drawBackground(Canvas c, Paint p, int left, int right, int top, int baseline, int bottom, CharSequence text, int start, int end, int currentLineNumber) { 
      final int textWidth = Math.round(p.measureText(text, start, end)); 
      final int paintColor = p.getColor(); 

      rect.set(left - paddingSize/2, top - paddingSize/4, left + textWidth + paddingSize/2, top + textView.getTextSize() + paddingSize/2); 
      p.setColor(backgroundColor); 
      c.drawRoundRect(rect, roundedCornerSize, roundedCornerSize, p); 
      p.setColor(paintColor); 
     } 
    } 

    int padding = textView.getPaddingLeft(); 
    int radius = padding/2; 

    SpannableStringBuilder builder = new SpannableStringBuilder(text); 
    BackgroundColorSpanWithPaddingAndLineSpacing backgroundSpan = new BackgroundColorSpanWithPaddingAndLineSpacing(backgroundColor, padding, radius); 
    builder.setSpan(backgroundSpan, 0, text.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); 

    textView.setShadowLayer(padding, 0, 0, 0); 
    textView.setLineSpacing(0, lineSpacingMultiplier); 

    textView.setText(builder, TextView.BufferType.SPANNABLE); 
} 

Verbrauch:

SpanUtils.setTextWithSpan(titleTv, android.graphics.Color.BLUE, textStr, 1.4f); 

Ich bin sicher, dass Sie nach Ihren Bedürfnissen zu modifizieren verwalten.