【发布时间】:2014-07-03 16:13:02
【问题描述】:
我有一个带有图标(按钮)的水平滚动条。
我尝试将选定的图标移动到栏的中间。
我已阅读post 关于在 android 中定位视图的内容。
我有这个代码,在我看来逻辑上没问题:
public void selectButton() {
...
HorizontalScrollView sv=(HorizontalScrollView)button.getParent().getParent();
int offsetX=getButtonXPosition()-sv.getWidth()/2;
sv.smoothScrollTo(offsetX, 0);
..
}
public int getButtonXPosition() {
return (button.getLeft()+button.getRight())/2;
}
我将左上角a 像素移动到右\左(负\正像素数),
其中a = X position of middle of selected button - middle of the bar。
意思是我想将按钮的中间a像素移动到右\左(负\正)
但是按钮停得太左\右(如果它是最右边\最左边的按钮)
见附件:
【问题讨论】:
-
也许你应该使用smoothScrollBy?您似乎在计算差异而不是实际位置。
-
@user1873880 我认为你是对的。但是a)它相对于(0,0)是不同的,所以没关系。 b)
smoothScrollBy让事情变得更加混乱
标签: java android xml user-interface scrollview