2014-04-08 18 views
8

Chciałbym pokazać tekst jak poniżej ...TextView z kolorem tła i odstępy między wierszami

enter image description here

Moje kodowania jest następująca:

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

Mam również ten sam problem. znalazłeś jakieś rozwiązanie? – saberrider

Odpowiedz

4

spróbować. Utwórz niestandardowy widok tekstu i przesłonięcie metody rysowania (płótno Canvas).

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

Minęło dużo czasu, ale zastosuję statyczną metodę poniżej. Mam nadzieję, że pomoże to drugiemu.

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

Zastosowanie:

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

Jestem pewien, że uda Ci się zmieniać w zależności od potrzeb.

Powiązane problemy