为各行添加内嵌式 CarIcon

您可以使用 CarIconSpan 添加内嵌文本的图标,从而提升应用的视觉吸引力。如需详细了解如何创建这些 span,请参阅有关 CarIconSpan.create 的文档。如需简要了解使用 span 设置文本样式的原理,请参阅使用 span 设置文本样式

val rating = SpannableString("Rating: 4.5 stars")
rating.setSpan(
    CarIconSpan.create(
        // Create a CarIcon with an image of four and a half stars
        CarIcon.Builder(
            IconCompat.createWithResource(carContext, R.drawable.ic_star)
        ).build(),
        // Align the CarIcon to the baseline of the text
        CarIconSpan.ALIGN_BASELINE
    ),
    // The start index of the span (index of the character '4')
    8,
    // The end index of the span (exclusive, length of the string)
    17,
    Spanned.SPAN_INCLUSIVE_INCLUSIVE
)

val row = Row.Builder()
    .setTitle("Rating Row")
    .addText(rating)
    .build()