选项卡主机使用水平scrollview-自动滚动 - 以编程方式

Nik*_*ddy 4 android horizontalscrollview android-tabhost

我正在开发一个TabHost包含的示例应用程序HorizontalScrollView.这是我的问题:

  • 我有超过10个标签,当我点击任何标签时,它应该将重力设置为中心?我怎么能来解决这个问题.

提前致谢!!!

ble*_*enm 5

这将涉及一些计算.

实际上,您需要滚动到位置以使元素居中.

int scrollX = (button.getLeft() - (screenWidth/2))+(button.getWidth()/2);
hScrollView.scrollTo(scrollX,0);
Run Code Online (Sandbox Code Playgroud)

在哪里

button -> It is the button you are trying to center.

screenWidth -> Width of the screen.
int screenWidth = getWindowManager().getDefaultDisplay().getWidth();

hScrollView -> It is the horizontal scrollview.
Run Code Online (Sandbox Code Playgroud)

但即使使用此方法,您也无法将末尾元素居中,因为scrollview不会滚动超出其边界.