Java 如果 TextView 大于 1 行,如何在我的 TextView 上显示省略号?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6393487/
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 can I show ellipses on my TextView if it is greater than the 1 line?
提问by Sheehan Alam
I have the following Layout which does not work:
我有以下布局不起作用:
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:id="@+id/experienceLayout"
android:background="#ffffff"
android:layout_height="match_parent"
android:paddingLeft="6dp"
android:paddingRight="6dp"
android:paddingBottom="6dp"
android:paddingTop="6dp">
<TextView
android:layout_weight="1"
android:id="@+id/experienceLabel"
android:text="Experience"
android:layout_height="wrap_content"
android:textColor="#000000"
android:layout_width="wrap_content"
android:textStyle="bold">
</TextView>
<TextView
android:id="@+id/experienceTextView"
android:text="TextView"
android:layout_height="wrap_content"
android:textColor="#000000"
android:layout_width="wrap_content"
android:ellipsize="end"
android:lines="1"
android:maxLines="1"
android:singleLine="true"
android:fadeScrollbars="false">
</TextView>
</LinearLayout>
采纳答案by BonanzaDriver
This is a common problem. Try using the following:
这是一个常见的问题。尝试使用以下方法:
android:scrollHorizontally="true"
android:ellipsize="end"
android:maxLines="1"
.............. the scrollHorizontally is the "special sauce" that makes it work.
................. scrollHorizontally 是使它起作用的“特殊调味料”。
回答by Atul Bhardwaj
This will also make a single line with ellipsise
这也将制作一条带有省略号的线条
android:singleLine="true"
回答by Marilia
The way it worked for me on multiple devices / APIs was programmatically like this (where tv is your TextView):
它在多个设备/API 上为我工作的方式以编程方式如下(其中 tv 是您的 TextView):
if (tv.getLineCount() > 1) {
int lineEndIndex = tv.getLayout().getLineEnd(0);
String text = tv.getText().subSequence(0, lineEndIndex - 3) + "\u2026";
tv.setText(text);
}
回答by Mohamed Ibrahim
Use this
用这个
android:ellipsize="end"
android:singleLine="true"
Don't use this without fully aware of what output comes
不要在不完全了解输出结果的情况下使用它
android:ellipsize="end"
android:maxLines="1"
When you use maxlines = 1
it will some time truncate most of the characters.
当您使用maxlines = 1
它时,它会在一段时间内截断大部分字符。
回答by SDK4551
So all the answers above cater to the requirement that only 1 line and then the ellipsis should appear. However if you want the ellipsis to appear after certain lines of text, then you should use the following:
所以上面的所有答案都满足了只有 1 行然后省略号应该出现的要求。但是,如果您希望省略号出现在某些文本行之后,则应使用以下内容:
android:ellipsize="end"
android:maxLines="2"
android:singleLine="false"
With this the ellipsis will appear only after 2 lines. Note: Its important to have singleLine as false.
这样,省略号只会出现在 2 行之后。注意:将 singleLine 设为 false 很重要。
回答by Edward Motloung
This helped me out:
这帮助了我:
android:ellipsize="end"
android:maxLines="1"
android:singleLine="true"
Make sure the TextView
width
is set to Match_Parent
确保TextView
width
设置为Match_Parent
https://github.com/chrisjenx/Calligraphy/issues/43#issuecomment-523701518
https://github.com/chrisjenx/Calligraphy/issues/43#issuecomment-523701518
回答by Reaz Murshed
android:singleLine
is deprecated. In my case, I had to get a fixed height for the TextView
and I used the android:lines
attribute instead of android:maxLines
. I thought this might help someone having the same problem as mine.
android:singleLine
已弃用。就我而言,我必须为 获得固定高度,TextView
并且我使用了android:lines
属性而不是android:maxLines
. 我认为这可能会帮助和我有同样问题的人。
android:ellipsize="end"
android:lines="2"