gpt4 book ai didi

android - 在不指定 maxLines 的情况下在 TextView 中椭圆化文本

转载 作者:太空宇宙 更新时间:2023-11-03 12:03:07 25 4
gpt4 key购买 nike

我有一个 TextView,其高度会根据屏幕中的其他组件而变化。我要在此 TextView 中设置很长的文本,因此我想将其省略。简单地指定 android:ellipsize="end"是行不通的。只有在指定 maxLines 时,椭圆化才会起作用。但是我不能为 maxLines 指定一个值,因为 TextView 的高度是动态的。如何在不为 TextView 指定 maxLines 的情况下使文本椭圆化?

最佳答案

我通过添加全局布局监听器找到了问题的解决方案。以下是代码 fragment

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);

}
});

关于android - 在不指定 maxLines 的情况下在 TextView 中椭圆化文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46642245/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com