android - 引号如何与 TextView 中的文本对齐?

标签 android textview spannablestring spannablestringbuilder

我有一个可跨越的字符串和一个包含图像的可跨越字符串 和字符串,但是当我在图像的 TextView 上显示该字符串时 与 textview 的基线对齐我想用它显示 ALIGN_TOP string 我卡在这上面了,请帮我解决一下。

这是我的代码

        String text2 = "  Lorem Ipsum is simply dummy text of the printing   and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.  ";

        Spannable spannable = new SpannableString(text2);

        spannable.setSpan(new ImageSpan(mActivity, R.mipmap.start_quote_green), 0, 1, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
        spannable.setSpan(new ImageSpan(mActivity, R.mipmap.end_quote_green), text2.length()-1, text2.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);

        inspirationalActivityInspirationalTextTv.setText(spannable, TextView.BufferType.SPANNABLE);

输出
result

必需
required

最佳答案

这里是答案

        String text2 = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.   ";

        Spannable spannable = new SpannableString(text2);
        spannable.setSpan(new TopImageSpan(mActivity, R.mipmap.start_quote_green), 0, 1, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
        spannable.setSpan(new TopImageSpan(mActivity, R.mipmap.end_quote_green), text2.length()-1, text2.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);


        inspirationalActivityInspirationalTextTv.setText(spannable, TextView.BufferType.SPANNABLE);

顶部与文本对齐的 TopImageSpan 类

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.support.annotation.NonNull;
import android.text.style.DynamicDrawableSpan;
import android.text.style.ImageSpan;

import java.lang.ref.WeakReference;

/**
 * Created by Humayoon on 8/10/17.
 */

public class TopImageSpan extends ImageSpan {

    private WeakReference<Drawable> mDrawableRef;
    public static boolean startQuote = false,EndQuote = true;

    public TopImageSpan(Context context, final int drawableRes) {
        super(context, drawableRes);
    }

    @Override
    public int getSize(Paint paint, CharSequence text,
                       int start, int end,
                       Paint.FontMetricsInt fm) {
        Drawable d = getCachedDrawable();
        Rect rect = d.getBounds();

        if (fm != null) {
            Paint.FontMetricsInt pfm = paint.getFontMetricsInt();
            // keep it the same as paint's fm
            fm.ascent = pfm.ascent;
            fm.descent = pfm.descent;
            fm.top = pfm.top;
            fm.bottom = pfm.bottom;
        }

        return rect.right;
    }

    @Override
    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 = getCachedDrawable();
        canvas.save();

        int drawableHeight = b.getIntrinsicHeight();
        int fontAscent = paint.getFontMetricsInt().ascent;
        int fontDescent = paint.getFontMetricsInt().descent;
        int transY = bottom - b.getBounds().bottom +  // align bottom to bottom
                (drawableHeight - fontDescent + fontAscent);  // align center to center

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

    // Redefined locally because it is a private member from DynamicDrawableSpan
    private Drawable getCachedDrawable() {
        WeakReference<Drawable> wr = mDrawableRef;
        Drawable d = null;

        if (wr != null)
            d = wr.get();

        if (d == null) {
            d = getDrawable();
            mDrawableRef = new WeakReference<>(d);
        }

        return d;
    }
}

关于android - 引号如何与 TextView 中的文本对齐?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45605794/

相关文章:

android - 有没有一种方法可以将 BackgroundColorSpan 设置为 SpannableString?

android - 弹性移动 : Different filenames for Android/iOS

java - 更改 navigationView 的单个特定菜单项的背景颜色

android - 如何防止 ImageView 重叠?

android - textview 不显示文本(一个 TextView 将另一个 TextView 推离屏幕很远)

java - 将标签添加到 ClickableSpan

android - 如何将 Androids Talkback 与 clickableSpan 一起使用?

java - 如何在Windows上设置Java路径?

c++ - Android设备 radio ROM编程

android - 检测 TextView 是否跨越 2 行