我想制作一个可通过用户触摸折叠的 TextView。
当 TextView 折叠时,我设置了 textView.setMaxLines(4);
。
如何在扩展方法中清除此状态?
我只能想到用 10000 之类的大数字调用 setMaxLines()
。
有没有更好的方法来实现这一点?
最佳答案
实际上,android 平台的做法是将 MaxLine 设置为 Integer.MAX_VALUE。
textView.setMaxLines(Integer.MAX_VALUE);
另外,如果您使用的是 Ellipsize,请不要忘记设置为 null。
textView.setEllipsize(null);
只需检查 android 框架是如何做到这一点的 ;) 观察 setMaxLines(Integer.MAX_VALUE);
private void applySingleLine(boolean singleLine, boolean applyTransformation) {
mSingleLine = singleLine;
if (singleLine) {
setLines(1);
setHorizontallyScrolling(true);
if (applyTransformation) {
setTransformationMethod(SingleLineTransformationMethod.getInstance());
}
} else {
setMaxLines(Integer.MAX_VALUE);
setHorizontallyScrolling(false);
if (applyTransformation) {
setTransformationMethod(null);
}
}
}
您可以在 Android 开源项目 (AOSP) 的源代码中找到它
https://source.android.com/source/downloading
如果你不想下载源代码,你可以在 github 上的镜像上查看源代码。
关于android - 重置Android textview maxlines,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5413394/