我有一个TextView
我正在动态添加文本。
in my main.xml
文件中我设置了属性以使我的最大行数为 19 且滚动条垂直。
in the .java
我正在使用的文件textview.setMovementMethod(new ScrollingMovementMethod());
以允许滚动。
滚动效果很好。一旦 19 行被占用,并且添加了更多行,它就会开始按预期滚动。问题是,我希望新文本滚动到视图中。
我正在写出的值textview.getScrollY()
它停留在0
无论如何(即使我手动向下滚动并添加新的文本行)。
最后textview.scrollTo(0, textview.getScrollY());
对我没有任何作用。
我是否应该使用另一种方法来获取垂直滚动量textview
?我读到的所有内容都表明,出于所有意图和目的,我所做的事情应该有效:/
对 TextView 源代码进行了一些挖掘,但这就是我想到的。它不需要您将 TextView 包装在 ScrollView 中,并且据我所知,它工作得很好。
// function to append a string to a TextView as a new line
// and scroll to the bottom if needed
private void addMessage(String msg) {
// append the new string
mTextView.append(msg + "\n");
// find the amount we need to scroll. This works by
// asking the TextView's internal layout for the position
// of the final line and then subtracting the TextView's height
final int scrollAmount = mTextView.getLayout().getLineTop(mTextView.getLineCount()) - mTextView.getHeight();
// if there is no need to scroll, scrollAmount will be <=0
if (scrollAmount > 0)
mTextView.scrollTo(0, scrollAmount);
else
mTextView.scrollTo(0, 0);
}
如果您发现失败的案例,请告诉我。我很高兴能够修复我的应用程序中的任何错误;)
编辑:我应该提到我也使用
mTextView.setMovementMethod(new ScrollingMovementMethod());
实例化我的 TextView 后。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)