Android 使用 maxLength 用 3 个点结束 TextView

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12686468/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 11:26:16  来源:igfitidea点击:

End TextView with 3 dots with use of maxLength

androidandroid-layouttextviewellipsize

提问by Yaniv

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.

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

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

我需要用 3 个点 (...) 结束这个 TextView,只有当我用 dp 给 layout_width 一个固定大小时才会发生这种情况,这是我不想做的事情。

I know it is possible to this by code and cut the string after the 15th character and then add the 3 dots, but I prefer to do that by XML.

我知道可以通过代码来实现这一点,并在第 15 个字符之后剪切字符串,然后添加 3 个点,但我更喜欢通过 XML 来做到这一点。

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

知道如何用 3 个点结束文本并将其保留为 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" />

回答by Kirk

This will solve your problem, Use ellipsizeProperty in your XML Code

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

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 -->

Edit:singleLineis deprecated. Use maxlines="1"instead.

编辑:singleLine弃用。使用maxlines="1"来代替。

回答by Ravi

I gather (from comment) that @Yaniv already solved it using code - but this is the right way to do it (with xml). May help other users who land here. Trick is to use both toleftof andtorightof.

我收集(从评论)@Yaniv 已经使用代码解决了它 - 但这是正确的方法(使用 xml)。可能会帮助其他登陆这里的用户。技巧是同时使用 toleftoftorightof。

<RelativeLayout>
...
 <TextView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:singleLine="true"
        android:layout_toRightOf="@id/some_element1"
        android:layout_toLeftOf="@id/some_element2"/>
...
<RelativeLayout>

回答by AZ_

You cannot use both maxLengthand Ellipsizealthough you can define Maximum EMSsee the example below

您不能同时使用两者maxLengthEllipsize尽管您可以定义,但Maximum EMS请参见下面的示例

<TextView
        android:id="@+id/tv_hist_source_lang"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ellipsize="end"
        android:maxEms="8"
        android:maxLines="1"
        android:text="TextView"
        android:textAppearance="?android:attr/textAppearanceMedium" />

It looks like this

看起来像这样

回答by Zhou Hongbo

You can use

您可以使用

android:maxWidth="100dp"
android:maxLines="1"
android:ellipsize="end"

Only this works for me.

只有这对我有用。

回答by Mahmoud Hashim

use this android:ellipsize="end"

用这个 android:ellipsize="end"

回答by Enzokie

One of the easiest way is to add Right Padding + Ellipsize

最简单的方法之一是添加Right Padding + Ellipsize

<TextView
            android:id="@+id/txtvw_contentcell_subhead"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Polem sampler, Lorem Ipsum golep tolem burop yemit noski"
            android:ellipsize="end"
            android:maxLines="1"
            android:textColor="@color/caption_black_color"
            android:textSize="@dimen/caption_size"
            android:paddingRight="40dp"/>

Here is an example Subtitle Text with char limit. enter image description here

这是一个带有字符限制的字幕文本示例。 在此处输入图片说明

回答by Horatio

You can just modify the Stringwhen it's length is above 20 to add the ellipsize.

String当它的长度大于 20 时,您可以修改它以添加椭圆大小。

回答by Benjamin Livinus

Very Important:ellipsize = "end"only works when maxLengthis set to a value that is more thanthe number of characters on one line. You can use wrap_contentif you align the TextView's end and start to any other view(s).

非常重要:ellipsize = "end"仅当maxLength设置为大于一行字符数的值时才有效 。wrap_content如果您将 TextView 的末端对齐并开始到任何其他视图,则可以使用。

回答by John T

Tried most of the solutions above. Using maxWidth was the key for me:

尝试了上面的大部分解决方案。使用 maxWidth 对我来说是关键:

android:maxWidth="100dp"
android:maxLines="1"
android:ellipsize="end"

回答by Micer

I needed to do this with Radio Button and I wanted to limit the size to one line. When I used Android:singleline="true", radio button's check circle disappeared. Here's the code that finally worked:

我需要用单选按钮来做到这一点,我想将大小限制为一行。当我使用时Android:singleline="true",单选按钮的检查圆圈消失了。这是最终有效的代码:

android:ellipsize="end"
android:maxLines="1"

There's no need to set ems. This will probably work also in TextView and other UI components. Hope this helps to someone.

没有必要设置ems。这可能也适用于 TextView 和其他 UI 组件。希望这对某人有帮助。