如何完全防止 TextView 滚动? [英] How do I completely prevent a TextView from scrolling?
问题描述
我有一个包含大量文本的 TextView.这个 TextView 设置了 maxLines
,所以它只显示前 8 行左右.我还有一个阅读更多"按钮,所以我自己处理扩展 TextView.
I have a TextView with a lot of text. This TextView has maxLines
set, so it only shows the first 8 or so lines. I also have a "Read More" button so I handle expanding the TextView on my own.
我的问题是有时 TextView 会滚动一点(一次只有半行),即使我从未指定任何滚动条.这个问题变得更糟了,因为TextView在一个ListView里面,所以当用户滚动主ListView时,TextView有时会滚动一点,像这样:
My problem is that sometimes the TextView scrolls a little (just half a line at a time), even though I never specified any scroll bars. This issue is made worse because the TextView is inside a ListView, so when the user scrolls the main ListView, the TextView sometimes scrolls a little, like this:
如何防止 TextView 滚动?
How do I prevent the TextView from scrolling?
推荐答案
我也有同样的问题,我的解决方法是创建一个 NoScrollTextView extends TextView 这样
I have the same problem,My solution is create a NoScrollTextView extends TextView like this
public class NoScrollTextView extends TextView {
public NoScrollTextView(Context context) {
super(context);
}
public NoScrollTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public NoScrollTextView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public NoScrollTextView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
@Override
public void scrollTo(int x, int y) {
//do nothing
}
}
设置滚动什么都不做
在 Kotlin 中:
In Kotlin:
class NonScrollingTextView : TextView {
constructor(context: Context) : super(context) {}
constructor(context: Context, attrs: AttributeSet) : super(context, attrs) {}
constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int) : super(context, attrs, defStyleAttr) {}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int, defStyleRes: Int) : super(context, attrs, defStyleAttr, defStyleRes) {
}
override fun scrollTo(x: Int, y: Int) {
//do nothing
}
}
这篇关于如何完全防止 TextView 滚动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!