Android 如何在textview中的文本末尾显示3个点
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21825803/
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
How to show 3 dots at the end of text in textview
提问by Anchit Mittal
I know this question is asked many times, i have gone through all the solutions but still the problem is same.
我知道这个问题被问了很多次,我已经完成了所有的解决方案,但问题仍然是一样的。
i want my text length to be of 50 characters. now it is coming in 2 lines.
我希望我的文本长度为 50 个字符。现在它有 2 行。
Below is what i did:
以下是我所做的:
<TextView
android:id="@+id/notification_Message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/notification_head"
android:layout_centerHorizontal="true"
android:layout_margin="10dp"
android:gravity="center"
android:ellipsize="end"
android:maxLength="50"
android:text=" Hello developer. please check this message for long length. max length to 50 characters. "
android:textSize="20sp" />
The text here is just a sample text i have added. it is not showing 3 dots at end rather showing message upto 50 characters. i have tried max linesalso, single line falsealso. but no positive result. I also don't want to handle this thing programmatically by adding three dots logically.
此处的文本只是我添加的示例文本。它没有在末尾显示 3 个点,而是显示最多 50 个字符的消息。我也试过最大行数,也试过单行假。但没有积极的结果。我也不想通过逻辑上添加三个点来以编程方式处理这件事。
Please help
请帮忙
回答by Ibrahim Yildirim
Make a preset width for your TextView and add these attributes (Shout-out to @T-D)
为您的 TextView 设置一个预设宽度并添加这些属性(Shout-out to @TD)
android:ellipsize="end"
android:maxLines="1"
The maxLines will make sure that your text wont be shown in two lines and the ellipsize will add the three dots in the end.
maxLines 将确保您的文本不会显示在两行中,而 ellipsize 将在最后添加三个点。
Old Answer
旧答案
android:ellipsize="end"
android:singleLine="true"
回答by Lokesh
For one line text with a dot but now it's Deprecatedso don't use singleLine
anymore
对于一个文本行以一个点,但现在它的过时,所以不要用singleLine
了
Thanks to this post I found that how to show dot after the end of text if we have more one line, it works for me :)
感谢这篇文章,我发现如果我们有多一行,如何在文本结束后显示点,它对我有用:)
android:ellipsize="end"
android:maxLines="2"
Post here this answer because all answer here are for single line not for multiline
在这里发布这个答案,因为这里的所有答案都是针对单行而不是针对多行的
回答by Manuel Spigolon
android:ellipsize
will works only with:
android:ellipsize
将仅适用于:
android:singleLine="true"
or
或者
android:maxLines="#any number#"
android:scrollHorizontally="true"
Because maxLength truncate the text.
因为 maxLength 会截断文本。
So you have to manage the label with code if you want to display only 50 char + dots, or let two line for the textview.
因此,如果您只想显示 50 个字符 + 点,或者让 textview 显示两行,则必须使用代码管理标签。
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="end"
android:maxLines="2"
android:scrollHorizontally="true"
android:text="Hello developer. please check this message for long length. max length to 50 characters."
android:textSize="20sp" />
回答by UmAnusorn
The android:singleLine="true"
is deprecated
在android:singleLine="true"
已被弃用
better use
更好地使用
android:maxLines="1"
android:maxLines="1"
and android:ellipsize="end"
to add "..."at the end of text
并在文本末尾android:ellipsize="end"
添加“...”
回答by Viswanath Lekshmanan
Use singleLine = "true"
for limit text by singleLine
使用singleLine = "true"
由单线限制文本
回答by Think Twice Code Once
The approach of @Lokesh works fine.
@Lokesh 的方法工作正常。
android:ellipsize="end"
android:maxLines="2"
But I realize a trouble that TextView will be truncated by word (in default). Show if we have a text like:
但是我意识到 TextView 会被单词截断的一个麻烦(默认情况下)。显示我们是否有这样的文本:
test long_line_without_any_space_abcdefgh
测试 long_line_without_any_space_abcdefgh
the TextView will display:
TextView 将显示:
test...
测试...
And I found solution to handle this trouble, replace spaces with the unicode no-break space character, it makes TextView wrap on characters instead of words:
我找到了解决这个问题的解决方案,用 unicode 不间断空格字符替换空格,它使 TextView 用字符而不是单词换行:
yourString.replace(" ", "\u00A0");
The result:
结果:
test long_line_without_any_space_abc...
测试 long_line_without_any_space_abc ...
回答by viral 9966
This is the answer what you want:
这是你想要的答案:
<TextView
android:id="@+id/tv_title"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="wrap_content"
android:text="Title"
android:maxLines="1"
android:ellipsize="end"
android:maxWidth="195dp"
android:textColor="@color/white"
android:textSize="14dp"
android:gravity="center"
/>
回答by Kshitij
I too was struggling with this. android:maxLength
wasn't having any effect so I played around with android:maxWidth
and it worked.
我也在为此苦苦挣扎。android:maxLength
没有任何效果,所以我玩了一下android:maxWidth
,它奏效了。
Checkout my snippet below.
在下面查看我的代码段。
<TextView
android:id="@+id/tv_comment_author"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxWidth="120dp"
android:singleLine="true"
android:ellipsize="end"
android:text="@string/dummy_text"/>
回答by Sanket Prabhu
Create your customize TextView:
创建您的自定义 TextView:
<com.xyz.CustomizedTextViewName
android:id="@+id/app_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:ellipsize="end"
android:singleLine="true"
android:maxEms="10"
app:typeface="roboto_bold"/>
回答by yuva ツ
<TextView
android:id="@+id/txt__customizer_item"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:gravity="center"
android:paddingRight="20dp"
android:singleLine="true"
android:text=" Hello developer. please check this message for long length. max length to 50 characters. "
android:textAppearance="@android:style/TextAppearance.Medium"
android:textColor="@color/light_blue"
android:textSize="20sp" />
This is working for me.give padding from right side.it will automatically shows ...
这对我有用。从右侧给填充。它会自动显示......