gpt4 book ai didi

android - ImageSpan.ALIGN_BASELINE 当 TextView 有 lineSpacing 时

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:15:34 24 4
gpt4 key购买 nike

我想将我的 ImageSpan 与文本的基线对齐,但我还需要在行之间添加一些间距。
问题是,当我添加行间距时,ImageSpan 没有与文本的基线对齐,而是与 baseline+lineSpacing 对齐,所以它看起来比它低应该。

有解决办法吗?

编辑:进一步说明:

  1. 它看起来如何没有 lineSpacing(箭头是ImageSpan)。它与基线正确对齐。
    enter image description here

  2. 如果我添加 android:lineSpacingMultiplier="1.2"
    会是什么样子 enter image description here

编辑2代码:
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" />

其他相关方法:

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

最佳答案

我最近遇到了同样的问题,设法通过更改 this answer 来解决它.我找不到如何从字体规范中获取行高,所以我将其设置为常量 float (您也可以从 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();
}
}

关于android - ImageSpan.ALIGN_BASELINE 当 TextView 有 lineSpacing 时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31539860/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com