如何在多行TextView的末尾插入ImageView?
也许这是一个愚蠢的问题,但我找不到在 TextView 第二行末尾插入小图像的方法。图像始终出现在整个文本视图的右侧,而不是行尾。
我想插入这样的图像:
TextTextTextTextTextTextTextTextTextText
TextTextTextText. <ImageView>
我得到的是:
TextTextTextTextTextTextTextTextTextText <ImageView>
TextTextTextText.
我希望有办法做到这一点。
来源:
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TESTESTESTESTESTESTESTESTESTESTESTESTES"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#FFFFFF" />
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/tv"
android:src="@drawable/icon" />
</RelativeLayout>
Maybe it's a stupid question, but I cant find a way to inser small image at the end of second line of TextView. Image is always appears to the right of whole textview, not the line end.
I want to insert image like this:
TextTextTextTextTextTextTextTextTextText
TextTextTextText. <ImageView>
What I am getting is:
TextTextTextTextTextTextTextTextTextText <ImageView>
TextTextTextText.
I hope there is way to do it.
Src:
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TESTESTESTESTESTESTESTESTESTESTESTESTES"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#FFFFFF" />
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/tv"
android:src="@drawable/icon" />
</RelativeLayout>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
创建一个 ImageSpan 并将其添加到您的文本视图中。这样,您就可以将图像放在文本中所需的位置
示例 -
create an ImageSpan and add it to your textview. This way, you can put your image where you want in the text
Example -
完成它的一种最佳方法如下......
One best way to complete it is as the following...
您可以将图像添加到字符串末尾,而无需使用 imageview,如下所示;
并称其为;
You can add the image to end of the string without using imageview like below;
and call it like;
如果您想在文本末尾插入 Drawable,Drawable 会隐藏文本的最后一个字符,以避免在文本末尾添加另一个字符并从该字符开始绘制。
If you want to insert Drawable at the end of the text, Drawable is hiding the last character of the text to avoid that add another character at the end of the text and start the drawable at that character.
TextView
类上的 Kotlin 扩展只需在
TextView
上设置文本后调用它即可。Kotlin extension on the
TextView
classJust call it after you set the text on the
TextView
and that's it.这可能不是最好的解决方案,但您可以尝试使用 Html.fromHtml 将图像插入到您的行中。
It might be not the best solution, but you can try using Html.fromHtml to insert image into your line.
感谢 Libin Thomas 我为
RatingBar
创建了一个解决方案。我使用了带有 SVG 星号的SvgRatingBar
。ImageSpan
扩展了DynamicDrawableSpan
,并且DynamicDrawableSpan
只有一个子级 -ImageSpan
。因此,要将另一个View
附加到TextView
的末尾,我们应该获得一个由View
制成的可绘制对象。我向SvgRatingBar
添加了一个方法getDrawable()
:创建评级布局 (layout_ rating_bar.xml):
然后编写以下代码:
Thanks to Libin Thomas I created a solution for
RatingBar
. I usedSvgRatingBar
with SVG stars.ImageSpan
extendsDynamicDrawableSpan
, andDynamicDrawableSpan
has only one child -ImageSpan
. So, to attach anotherView
to the end of theTextView
we should get a drawable made from theView
. I added a methodgetDrawable()
toSvgRatingBar
:Create a layout for the rating (layout_rating_bar.xml):
Then write this code: