Java 如何让你停在我想停的地方?
我有一个水平滚动条,上面有图标(按钮) 我尝试将所选图标移动到栏的中间 我读过关于在android中定位视图的文章 我得到了这个代码,在逻辑上我觉得还行:Java 如何让你停在我想停的地方?,java,android,xml,user-interface,scrollview,Java,Android,Xml,User Interface,Scrollview,我有一个水平滚动条,上面有图标(按钮) 我尝试将所选图标移动到栏的中间 我读过关于在android中定位视图的文章 我得到了这个代码,在逻辑上我觉得还行: public void selectButton() { ... HorizontalScrollView sv=(HorizontalScrollView)button.getParent().getParent(); int offsetX=getButtonXPosition()
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所选按钮中间位置
-
条中间位置
这意味着我想将按钮a
像素的中间部分向右/向左移动(负/正)
但是按钮停止得太左\太右(如果是最右\最左的按钮)
见附图:
试试这个
<HorizontalScrollView
android:id="@+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true">
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_margin="10dp">
</LinearLayout>
</HorizontalScrollView>
for(int x=0;x
也许你应该使用smoothScrollBy?看起来你是在计算差异而不是实际位置。@user1873880我以为你是对的。但是a)它相对于(0,0)是不同的,所以它不重要。b) smoothScrollBy
让事情变得更糟你能解释一下吗?
for (int x = 0; x < ImagesList.size(); x++) {
imageHor = new ImageView(FullIMageScreenNew.this);
linearLayout.setTag(x);
imageHor.setImageBitmap(decodeSampledBitmapFromResource(ImagesList.get(x)));
LinearLayout.LayoutParams llp = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
llp.setMargins(10, 10, 10, 10); // 4 margin values for Top/Left/Right/Bottom
linearLayout.addView(imageHor, llp);
}
linearLayout.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
for (int i = 0; i < ImagesList.size(); i++) {
indexNumber.add(i);
int index = indexNumber.indexOf(i);
Log.e("IndexValue=====", "" + index);
awesomePager.setCurrentItem(index, true);
}
}
});