2015-07-21 6 views
5

Chcę dopasować mój ImageSpan do linii bazowej tekstu, ale muszę również dodać odstępy między liniami.
Problem polega na tym, że po dodaniu odstępów między wierszami ImageSpan nie wyrównuje się do linii bazowej tekstu, ale do baseline+lineSpacing, więc wydaje się być niższy niż powinien.ImageSpan.ALIGN_BASELINE, gdy TextView ma lineSpacing

Czy istnieje obejście tego problemu?

Edit: Dalsze wyjaśnienia:

  1. Jak to wygląda bezlineSpacing (strzałka jest ImageSpan). Jest poprawnie wyrównany do linii podstawowej.
    enter image description here

  2. Jak to wygląda jeśli dodam android:lineSpacingMulitiplier="1.2"
    enter image description here

Edycja 2 Kod:
XML:

<com.kushtrim.example.views.CustomTypefaceTextView 
    android:id="@+id/descId" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:lines="3" 
    android:gravity="center_vertical" 
    android:layout_marginLeft="@dimen/_45" 
    android:layout_marginTop="@dimen/_6" 
    android:layout_marginBottom="@dimen/_20" 
    app:font_type="merriweather_regular" 
    android:textSize="@dimen/f40" 
    android:lineSpacingMultiplier="1.2" 
    android:textColor="@color/black" 
    android:text="Lorem ipsum dolor sit amet, consectetur adipiscing elit." 
    android:ellipsize="end" /> 

Inne odpowiednie metody:

private Spannable getContentText(ContactRaport contactRaport) { 
    DateTime dateTime = new DateTime(contactRaport.contactDate); 

    String datePart = dateTime.getDayOfMonth() + " " + dateTime.monthOfYear().getAsShortText(new Locale("nl")) + "; "; 
    String completeText = datePart + contactRaport.note; 

    Typeface font1 = Typeface.createFromAsset(context.getAssets(), "MyFont1.ttf"); 
    Typeface font2 = Typeface.createFromAsset(context.getAssets(), "MyFont2.ttf"); 
    SpannableStringBuilder spannable = new SpannableStringBuilder("XX"); 
    ImageSpan arrow = getArrowImageSpan(contactRaport); 
    spannable.setSpan(arrow, 0, 2, Spannable.SPAN_INCLUSIVE_EXCLUSIVE); 
    spannable.append(completeText); 

    spannable.setSpan(new CustomTypefaceSpan("", font1), 2, datePart.length()+1, Spanned.SPAN_EXCLUSIVE_INCLUSIVE); 
    spannable.setSpan(new CustomTypefaceSpan("", font2), datePart.length(), completeText.length(), Spanned.SPAN_EXCLUSIVE_INCLUSIVE); 
    spannable.setSpan(new ForegroundColorSpan(getContentDateColor(contactRaport)),2, datePart.length()+1, 0); 

    return spannable; 
} 

.

private ImageSpan getArrowImageSpan(ContactRaport contactRaport) { 

    Drawable d = null; 
    switch (contactRaport.type) { 
     ... logic to load the correct drawable 
    } 


    d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight()); 

    return new ImageSpan(d, ImageSpan.ALIGN_BASELINE); 
} 
+0

pokaż mi, co u kod dokładnie want..provide ya screen short – Destro

+0

@Destro Dalszy opis tego, co mam na myśli Nie napisałem kodu, ponieważ nie robię nic specjalnego, po prostu dodając android: lineSpacingMulitiplier –

+0

jak dodać obraz, czyli pod układem liniowym ya układ względny .. jest dodany w środku w pionie układu – Destro

Odpowiedz

0

miałem ten sam problem ostatnio udało się rozwiązać go zmieniając this answer. Nie mogłem dowiedzieć się, jak uzyskać wysokość linii od parametrów czcionki tak ustawić go jako stały pływaka (można też dostać go od TextView.

private static class CenterImageSpan extends ImageSpan { 

    public CenterImageSpan(Drawable d) { 
     super(d, ALIGN_BOTTOM); 
    } 

    public void draw(@NonNull Canvas canvas, CharSequence text, int start, 
        int end, float x, int top, int y, int bottom, 
        @NonNull Paint paint) { 
     Drawable b = getDrawable(); 
     canvas.save(); 

     int transY = bottom - b.getBounds().bottom; 
     // this is the key 
     transY -= paint.getFontMetricsInt().descent/2; 

     // adjust it for the current line height 
     transY *= 1 - (LINE_HEIGHT - 1) * 2; 

     canvas.translate(x, transY); 
     b.draw(canvas); 
     canvas.restore(); 
    } 
}