在多行TextView的末尾放置一个视图

时间:2020-07-28 09:15:17

标签: android android-layout android-view android-xml

我想在动态TextView的末尾添加一个自定义视图(带有文本和图标的视图),如图所示。

谢谢。

enter image description here

3 个答案:

答案 0 :(得分:2)

可以通过编程实现:

fun TextView.setIconifiedText(text: String, @DrawableRes iconResId: Int) {
    SpannableStringBuilder("$text#").apply {
        setSpan(
            ImageSpan(context, iconResId, DynamicDrawableSpan.ALIGN_BOTTOM),
            text.length,
            text.length + 1,
            Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
        )
    }.let {
        setText(it)
    }
}
textView.setIconifiedText(
    "First line text that is longer.\nSecond line text.",
    R.drawable.ic_android_black_24dp
)

结果:

答案 1 :(得分:0)

您需要使用android:drawableEnd

答案 2 :(得分:0)

将它们都放置在相对布局中并添加

android:layout_toEndOf="<id of first view>"

进入自定义视图