在SpannableString中放大单个字符会影响行间距(仅限Marshmallow)

Che*_*eng 3 android

我有以下方法,它扩大了第一个字符TextView.

private void makeFirstLetterBig(TextView textView, String title) {
    final SpannableString spannableString = new SpannableString(title);
    int position = 0;
    spannableString.setSpan(new RelativeSizeSpan(2.0f), position, position + 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    //textView.setText(spannableString.toString());
    textView.setText(spannableString, BufferType.SPANNABLE);
}
Run Code Online (Sandbox Code Playgroud)

这是TextView正在使用的.

<TextView
    android:id="@+id/title_text_view"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="16sp"
    android:textColor="?attr/newsTitleTextViewSelector"
    android:duplicateParentState="true"
    android:text="TextView" />
Run Code Online (Sandbox Code Playgroud)

这是Marshmallow 6之前的结果.

在此输入图像描述

它看起来很漂亮,因为当文本内容包装到第二行时,大的第一个字符不会影响第二行.

但是,当在Marshmallow 6中运行相同的应用程序时,我得到以下结果.

在此输入图像描述

看起来,大字"我"正在为整个文本内容创建一个大填充.这导致第二行(与亚马逊)与第一行有一个巨大的行间距.

我可以知道,我怎样才能在棉花糖6中避免这样的问题?我希望得到与棉花糖之前相同的结果.

p/s我也在https://code.google.com/p/android/issues/detail?id=191187上提交了一份报告.

更新

2015年12月9日,Google已修复此问题,并将在Android 6.0.1中发布 - https://code.google.com/p/android/issues/detail?id=191187

Mat*_*ini 6

TL; DR问题是由方法generate中的错误引起的StaticLayout.

该bug与我最初的想法略有不同,它不会影响所有行,只会影响放大后的字符,如截图所示:

在此输入图像描述

在Marshmallow FontMetrics中添加了缓存以避免重新计算,如源代码中的注释所解释的:

// measurement has to be done before performing line breaking
// but we don't want to recompute fontmetrics or span ranges the
// second time, so we cache those and then use those stored values
Run Code Online (Sandbox Code Playgroud)

遗憾的fm.descent是,如果缓存中的可用值低于之前的缓存值,则会丢弃缓存中可用的值,如您在代码段中所示:

for (int spanStart = paraStart, spanEnd; spanStart < paraEnd; spanStart = spanEnd) {
    // retrieve end of span
    spanEnd = spanEndCache[spanEndCacheIndex++];

    // retrieve cached metrics, order matches above
    fm.top = fmCache[fmCacheIndex * 4 + 0];
    fm.bottom = fmCache[fmCacheIndex * 4 + 1];
    fm.ascent = fmCache[fmCacheIndex * 4 + 2];
    fm.descent = fmCache[fmCacheIndex * 4 + 3];
    fmCacheIndex++;

    if (fm.top < fmTop) {
        fmTop = fm.top;
    }
    if (fm.ascent < fmAscent) {
        fmAscent = fm.ascent;
    }
    if (fm.descent > fmDescent) {
        fmDescent = fm.descent;
    }
    if (fm.bottom > fmBottom) {
        fmBottom = fm.bottom;
    }

    ....
Run Code Online (Sandbox Code Playgroud)

一旦该值达到最大值,它将永远不会减少,这就是为什么每个后续行都有增加的行空间.