如何使用 maxLength 以 3 个点结束 TextView [英] How to end TextView with 3 dots by using maxLength

查看:11
本文介绍了如何使用 maxLength 以 3 个点结束 TextView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的布局中有一个 TextView,它是 layout_width 中的 wrap_content.它限制为最多 15 个字符,所以我使用 maxLength.

I have a TextView in my layout which is wrap_content in layout_width. It is limited to maximum of 15 characters so I'm using maxLength.

我需要用 3 个点 (...) 结束此 TextView 并且仅当我在 dp 中为 layout_width 提供固定大小时才会发生这种情况,我不这样做不想做.

I need to end this TextView with 3 dots (...) and it happens only when I give the layout_width a fixed size in dp, something that I don't want to do.

我知道通过在第 15 个字符之后修剪字符串然后添加 3 个点以编程方式是可能的,但我更喜欢通过 XML 来做到这一点.

I know it is possible programmatically by trimming the string after the 15th character and then adding the 3 dots, but I prefer to do that by XML.

知道如何用 3 个点结束文本并将其保留为 wrap_content 吗?

Any idea how to end the text with 3 dots and leave it wrap_content?

<TextView
    android:id="@+id/inbox_contactName"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:lines="1"
    android:maxLines="1"
    android:ellipsize="end"
    android:singleLine="true"
    android:maxLength="15"
    android:textColor="#0670b4"
    android:textSize="16sp" />

推荐答案

这将解决您的问题,在您的 XML 代码中使用 ellipsize 属性

This will solve your problem, Use ellipsize Property in your XML Code

android:ellipsize="end" <!-- This makes the magic ... thing -->
android:maxEms="15" <!-- Limit of the Text -->
android:singleLine="true" <!-- In case if you want everything in one line -->

singleLine弃用.请改用 maxlines="1".

这篇关于如何使用 maxLength 以 3 个点结束 TextView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆