Android的计算HorizontalScrollView scrollTo位置 [英] Android calculate scrollTo position in HorizontalScrollView
问题描述
我有HorizontalScrollView很少TextViews。每个TextView中可能包含不同长度的文本。
I have HorizontalScrollView with few TextViews. Each TextView might contain text of different length.
我应该如何计算正确的偏移量滚动到使用scrollTo(X,Y)?
How should I calculate correct offset to scroll to using "scrollTo(x, y)"?
滚动运作,但不同的预期。 我已经试过各种事情(getLocationOnScreen将(),getMeasuredWidth()的getWidth()每个子元素,并将它们添加到togetehr),但我仍然不能得到结果,我喜欢。查看滚动有点过头或不足。
Scrolling works, but different than expected. I've tried various things (getLocationOnScreen(), getMeasuredWidth(), getWidth() on each child element and them adding them togetehr) but I still can't get result I like. View is scrolled a bit too far or not enough.
解决方案
在HSV具有不同宽度的多个元素,你可以对他们每个这样的中心:
When HSV has multiple elements with different width you can center on each of them like this:
for (TextView el : requiresFocus) {
if (el.getText().equals(requiresFocus)) {
offsetX = ((el.getLeft() + el.getRight()) / 2) - (myHSV.this.getWidth() / 2);
scrollTo(offsetX, 0);
break;
}
}
与((el.getLeft()+ el.getRight())/ 2)
你会得到元素的中心和( myHSV.this.getWidth()/ 2)
您将得到屏幕的中央。
with ((el.getLeft() + el.getRight()) / 2)
you will get center of the element and with (myHSV.this.getWidth() / 2)
you will get center of the screen.
推荐答案
试试这个:
int x, y;
x = myTextView.getLeft();
y = myTextView.getTop();
myHsv.scrollTo(x, y);
getLeft()
和共达()
应该返回相对于父视图的坐标(即, HorizontalScrollView
在这种情况下。
getLeft()
and getTop()
should return the coordinates relative to the parent view (that is, the HorizontalScrollView
in this instance.
这篇关于Android的计算HorizontalScrollView scrollTo位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!