Android TextView 使每行的字体大小不同

Android TextView making the font different size per line

我想使用 TextView

第一行,我希望字体是24dp 第二行,我希望字体是16dp

这可以用 1 个 TextView 完成吗,还是我需要创建其中的 2 个?

可以在 1 个 TextView 中使用 SpannableString 完成

来自https://developer.android.com/reference/android/text/style/RelativeSizeSpan

您可以使用:

SpannableString string = new SpannableString("Text with relative size span");
string.setSpan(new RelativeSizeSpan(1.5f), 10, 24, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); 

其中 1.5f 将增加 50%,10 为起始索引,24 为结束索引

public void setSpan (Object what, 
                int start, 
                int end, 
                int flags)

这里还有一个例子Different font size of strings in the same TextView