应用ellipsize时,获取textview中可见字符的数量

问题描述 投票:1回答:1

当我们在TextView中应用android:ellipsize时,我想得到TextView中可见的字符数。假设我有“这是多线的虚拟头条故事标题,这是多线的假顶级故事标题。”文本和我已经在我的"android:ellipsize="end" android:maxLines="2""中设置Textview这些属性,因此它将减少一些文本并显示3个点。现在我想要可见的字符数量。

android textview
1个回答
0
投票
  • 在singleLine TextView的情况下: stringLength - textView.length()

会给出在设备中没有显示或删除多少文本。

  • 在多线TextView的情况下,您可以使用相同的方式: 布局textViewLayout = textview.getLayout(); textViewLayout.getEllipsisCount(textViewLayout.getLineCount() - 1)

根据developer.android.com的文档

getEllipsisCount(int line)

Returns the number of characters to be ellipsized away, or 0 if no ellipsis is to take place.

注意:在textview可见/绘制后必须使用getEllipsisCount

© www.soinside.com 2019 - 2024. All rights reserved.