在 TextView 中将 drawable 设置为文本的末尾

Set drawable to the end of the text, in TextView

我想将可绘制图像设置到文本的末尾,但它显示了文本视图的末尾。

这是我的代码

<TextView
     android:id="@+id/tv_overweight_messsage"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content
     android:layout_marginTop="@dimen/activity_marginVertical_Large"
     android:gravity="center"
     android:text="dvdmnvkdnxknb vcnbvcxmnbxm,cbn,m vnnxmndxvbhddbv svbhzv"
     android:textColor="@color/required"
     android:textSize="@dimen/textSize_small"
     android:drawablePadding="@dimen/widget_padding"
     android:drawableRight="@drawable/img_infobn"
     android:visibility="gone"
     app:mfafont="@string/font_Questrial_Regular"
     tools:visibility="visible" />

您可以嵌入 ImageSpan in a SpannableString and set the TextView 来显示它。例如:

String text = "hello  ";
SpannableStringBuilder sb = new SpannableStringBuilder(text);
ImageSpan imageSpan = new ImageSpan(context, R.drawable.my_icon);
sb.setSpan(imageSpan, text.length() - 1, text.length()), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

检查这个 -

 <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:drawableRight="@drawable/ic_arrow_back_black_24dp"
            android:gravity="center"
            android:paddingLeft="10dp"
            android:paddingRight="10dp"
            android:drawablePadding="10dp"
            android:text="text"/>

尝试设置

android:layout_width="match_parent"

您需要使用 android:drawableEnd 而不是 drawableRight