我想创建一个可以通过用户触摸折叠的TextView.当TextView折叠时,我设置了textView.setMaxLines(4);.如何在我的expand方法中清除此状态?我只能想到setMaxLines()一个像10000这样的值大号的调用.
有没有更好的方法来实现这一点?
Val*_*mar 100
实际上,android平台的做法是将MaxLine设置为Integer.MAX_VALUE.
textView.setMaxLines(Integer.MAX_VALUE);
Run Code Online (Sandbox Code Playgroud)
另外,如果您使用的是Ellipsize,请不要忘记设置为null.
textView.setEllipsize(null);
Run Code Online (Sandbox Code Playgroud)
只检查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);
}
}
}
Run Code Online (Sandbox Code Playgroud)
你可以在Android开源项目(AOSP)的源代码中找到这个
https://source.android.com/source/downloading
如果您不想下载源代码,可以在github上的镜像上查看源代码.
试试这个(infoView.getLineCount()):
public void onMoreClick(View v) {
Button btn = (Button) v;
if(!moreSwitcher) {
infoView.setMaxLines(infoView.getLineCount());
infoView.setLines(infoView.getLineCount());
moreSwitcher = true;
btn.setText(R.string.collapse);
}else{
infoView.setMaxLines(5);
infoView.setLines(5);
moreSwitcher = false;
btn.setText(R.string.expand);
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
21664 次 |
| 最近记录: |