动态字符串的不同字体大小

时间:2018-10-09 14:02:37

标签: android font-size spannablestring imagespan spannablestringbuilder

我有一个TextView,使用SpannableStringBuilder填充了2个字符串和一个图像。如下所示:-

    SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder();
    StringBuilder stringBuilder = new StringBuilder(Util.getFormattedText(getProfileName()) + " ");
    RelativeSizeSpan largeSizeText = new RelativeSizeSpan(5.0f);
    stringBuilder.append(m_context.getResources().getString(R.string.extra_dots));// added two extra char to display image
    Spannable span = new SpannableString(stringBuilder);
    Drawable mDrawableDot = m_context.getResources().getDrawable(R.drawable.ic_comment_separator_dot);
    mDrawableDot.setBounds(m_context.getResources().getInteger(R.integer.value_0), m_context.getResources().getInteger(R.integer.value_0), m_context.getResources().getInteger(R.integer.dot_size), m_context.getResources().getInteger(R.integer.dot_size));
    ImageSpan imageSpan = new ImageSpan(mDrawableDot, ImageSpan.ALIGN_BASELINE){

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

            int transY = bottom - b.getBounds().bottom;
            // this is the key
            transY -= paint.getFontMetricsInt().descent * m_context.getResources().getInteger(R.integer.value_2);

            canvas.translate(x, transY);
            b.draw(canvas);
            canvas.restore();
        }
    };
    span.setSpan(imageSpan, span.length() - 2, span.length(), Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
    spannableStringBuilder.append(span);
    spannableStringBuilder.append(" " + getDate());



    tvUsername.setText(spannableStringBuilder);

现在,文本可以正确显示。现在,我想在ImageSpan之前增加文本的大小

我尝试做

 span.setSpan(new RelativeSizeSpan(2f), 0,5, 0); // set size

但这只会增加图像前字符串的第一个单词。如何在图像前增加整个字符串的大小?任何帮助表示赞赏。

0 个答案:

没有答案