2016-04-04 20 views
0

Wie kann ich einen solchen Effekt mit einem Android EditText erzielen? Es sieht irgendwie wie ausgewählter Text aus und ich konnte etwas Ähnliches in der API nicht finden.EditText Hintergrund nur für Text?

Dies ist keine Hintergrundfarbe für die Ansicht, sondern eine Hintergrundfarbe nur für den Text. Sie können sehen, wie es bei Zeilenumbrüchen stoppt und eine dünne weiße Linie zwischen Textzeilen hat.

Ich habe versucht mit Spannable, aber neue Zeile Trennzeichen nicht angezeigt Es ist die Auswahl aller Text ohne jede Lücke.

Erwartet

enter image description here

Erworbene

enter image description here

-Code

Spannable spannable = mNote.getText(); 
spannable.setSpan(new BackgroundColorSpan(ContextCompat.getColor(ThankYouNoteActivity.this, R.color.colorPrimaryDark));, 0, mNote.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); 
+0

Verwendung 'android.text.style.LineBackgroundSpan' – pskink

+0

prüfen diese [SO link] (http://stackoverflow.com/a/ 10676390/3831557) für die Implementierung –

+0

Hallo Pskink danke für Ihren Kommentar können Sie das Konzept als Antwort für diese Abfrage erarbeiten? – Christ

Antwort

-1

erstellen Sie ein XML in den Zeichnungsordner wie unten und legen Sie die Farben wie Sie möchten. Legen Sie dann einen Hintergrund für den EditText fest.

<?xml version="1.0" encoding="utf-8"?> 
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item> 
     <shape android:shape="rectangle"> 
      <solid android:color="@android:color/transparent" /> <!--background color of box--> 
     </shape> 
    </item> 

    <item 
     android:top="-2dp" 
     android:right="-2dp" 
     android:left="-2dp"> 
     <shape> 
      <solid android:color="@android:color/transparent" /> 
      <stroke 
       android:width="1dp" 
       android:color="#000000" /> <!-- color of stroke --> 
     </shape> 
    </item> 
</layer-list> 
1
public class MyLineBackgroundSpan 
    implements LineBackgroundSpan { 

private int backgroundColor = 0; 
private int padding = 0; 

public MyLineBackgroundSpan(int backgroundColor, int padding) { 
    this.backgroundColor = backgroundColor; 
    this.padding = padding; 
} 

@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 lnum) { 
    final int textWidth = Math.round(p.measureText(text, start, end)); 
    final int paintColor = p.getColor(); 
    final Paint.FontMetrics fm = p.getFontMetrics(); 

    // using fontMetrics is to free to lineSpacingExtra or includeFontPadding of TextView attributes. 

    RectF rect = new RectF(left - padding, 
      baseline + fm.ascent - padding, 
      left + textWidth + padding, 
      baseline + fm.descent + padding); 

    p.setColor(backgroundColor); 
    c.drawRect(rect, p); 

    p.setColor(paintColor); 
} 
} 

Probe

see

int padding = 10; 

SpannableString spannable = new SpnnableString("..."); 
spannable.setSpan(
     new MyLineBackgroundSpan(ContextCompat.getColor(context, R.color.colorPrimaryDark), padding), 
     0, spannable.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); 

textView.setShadowLayer(padding, 0, 0, 0); 
textView.setPadding(padding, padding, padding, padding); 
textView.setText(spannable);