2015-07-21 3 views
5

मैं टेक्स्ट की आधार रेखा पर अपना ImageSpan संरेखित करना चाहता हूं, लेकिन मुझे लाइनों के बीच कुछ अंतर जोड़ने की भी आवश्यकता है।
समस्या यह है कि जब मैं लाइन स्पेसिंग जोड़ता हूं, तो ImageSpan टेक्स्ट की आधार रेखा से संरेखित नहीं होता है, लेकिन baseline+lineSpacing पर, इसलिए यह उससे कम दिखाई दे रहा है।ImageSpan.ALIGN_BASELINE जब टेक्स्ट व्यू में लाइन है

क्या इसके लिए कोई कामकाज है?

संपादित करें: आगे स्पष्टीकरण:

  1. बिना तरहlineSpacing यह कैसा दिखता है (तीर ImageSpan है)। यह आधार रेखा से सही ढंग से गठबंधन है।
    enter image description here

  2. यह कैसे की तरह अगर मैं जोड़ने android:lineSpacingMulitiplier="1.2"
    enter image description here

संपादित 2 कोड दिखता है:
एक्सएमएल:

<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" /> 

अन्य प्रासंगिक तरीके:

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

मुझे क्या यू वास्तव में want..provide कोड दिखाने या स्क्रीन शॉर्ट – Destro

+0

@ डेस्ट्रो ने आगे बताया कि मेरा क्या मतलब है। मैंने कोड पोस्ट नहीं किया है क्योंकि मैं कुछ भी विशेष नहीं कर रहा हूं, बस एंड्रॉइड जोड़ रहा हूं: lineSpacingMulitiplier –

+0

छवि जोड़ने के लिए, रैखिक लेआउट या सापेक्ष लेआउट के तहत मतलब है .. छवि लेआउट के ऊर्ध्वाधर में केंद्र में जोड़ दिया गया है। – Destro

उत्तर

0

मैं हाल ही में एक ही समस्या थी, this answer फेरबदल के साथ इसे हल करने में कामयाब रहे। मैं बाहर कैसे फ़ॉन्ट मीट्रिक से पंक्ति की ऊंचाई प्राप्त करने के लिए तो मैं एक निरंतर नाव के रूप में सेट नहीं मिला (आप भी 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(); 
    } 
} 
संबंधित मुद्दे