如果 TextView 大于 1 行,如何在它上显示省略号?
我有以下不起作用的布局:
<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>
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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
这是一个常见问题。尝试使用以下内容:
........................滚动水平是使其发挥作用的“特殊酱汁”。
This is a common problem. Try using the following:
.............. the scrollHorizontally is the "special sauce" that makes it work.
这也将制作一条带有省略号的单线
This will also make a single line with ellipsise
使用此
在没有完全了解输出内容的情况下不要使用此
当您使用
maxlines = 1
时,它有时会截断大部分字符。Use this
Don't use this without fully aware of what output comes
When you use
maxlines = 1
it will some time truncate most of the characters.它在多个设备/API 上为我工作的方式以编程方式如下(其中 tv 是您的 TextView):
The way it worked for me on multiple devices / APIs was programmatically like this (where tv is your TextView):
所以上面的所有答案都满足只出现 1 行然后省略号的要求。但是,如果您希望省略号出现在某些文本行之后,那么您应该使用以下命令:
这样,省略号将仅出现在 2 行之后。注意:将 singleLine 设置为 false 很重要。
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:
With this the ellipsis will appear only after 2 lines. Note: Its important to have singleLine as false.
这帮助了我:
确保将
TextView
width
设置为Match_Parent
https://github.com/chrisjenx/Calligraphy/issues/43#issuecomment-523701518
This helped me out:
Make sure the
TextView
width
is set toMatch_Parent
https://github.com/chrisjenx/Calligraphy/issues/43#issuecomment-523701518
android:singleLine
已弃用。就我而言,我必须为TextView
获取固定高度,并且我使用android:lines
属性而不是android:maxLines
。我想这可能会帮助那些和我有同样问题的人。android:singleLine
is deprecated. In my case, I had to get a fixed height for theTextView
and I used theandroid:lines
attribute instead ofandroid:maxLines
. I thought this might help someone having the same problem as mine.