[英]ImageSpan.ALIGN_BASELINE when TextView has lineSpacing
I want to align my ImageSpan
to the baseline of the text, but I also need to add some spacing between the lines. 我想将ImageSpan
与文本的基线对齐,但是我还需要ImageSpan
之间添加一些间距。
The problem is that when I add line spacing, the ImageSpan
doesn't align to the baseline of the text, but to the baseline+lineSpacing
, so it's appearing lower than it should. 问题是,当我添加行距时, ImageSpan
不会与文本的基线对齐,而是与baseline+lineSpacing
,因此它看起来比应该的要低。
Is there a workaround to this ? 有解决方法吗?
Edit: Further explanation : 编辑:进一步的解释:
How it looks like without lineSpacing
( the arrow is the ImageSpan
). 没有 lineSpacing
时的外观(箭头是ImageSpan
)。 It's correctly aligned to the baseline. 它与基线正确对齐。
How it looks like if I add android:lineSpacingMulitiplier="1.2"
如果添加android:lineSpacingMulitiplier="1.2"
它的外观如何
Edit 2 The code: 编辑2代码:
XML: 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" />
Other relevant methods: 其他相关方法:
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);
}
I had the same problem lately, managed to solve it with altering this answer . 我最近遇到了同样的问题,通过更改此答案设法解决了这个问题 。 I couldn't find out how to get the line height from font metrics so I set it as a constant float (you can also get it from the TextView. 我无法找到如何从字体指标中获取行高,因此将其设置为恒定浮点数(也可以从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();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.