不指定maxLines的TextView中的省略号文本 [英] Ellipsize text in TextView without specifying maxLines
问题描述
我有一个TextView,其高度将根据屏幕中的其他组件而变化.我在此TextView中设置了一个长文本,因此我想将其省略.简单地指定android:ellipsize ="end"不起作用.仅在与maxLines一起指定时,椭圆化起作用.但是我无法为maxLines指定值,因为TextView的高度是动态的.如何在不为TextView指定maxLines的情况下将文本椭圆化?
I have a TextView whose height would vary based on the other components in the screen. I have a long text to be set in this TextView and hence I would like to ellipsize it. Simply specifying android:ellipsize="end" is not working. Only on specifying maxLines along with it, the ellipsizing works. But I cannot specify a value for maxLines since the height of TextView is dynamic. How do I get the text ellipsized without specifying maxLines for the TextView?
推荐答案
我通过添加全局布局侦听器找到了解决该问题的方法.以下是代码段
I found the solution to the problem by adding global layout listener. Following is the code snippet
tvDesc.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
tvDesc.getViewTreeObserver().removeOnGlobalLayoutListener(this);
int noOfLinesVisible = tvDesc.getHeight() / tvDesc.getLineHeight();
tvDesc.setText(R.string.desc);
tvDesc.setMaxLines(noOfLinesVisible);
tvDesc.setEllipsize(TextUtils.TruncateAt.END);
}
});
这篇关于不指定maxLines的TextView中的省略号文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!