我正在开发一个使用
TabHost
的示例应用程序,其中包含HorizontalScrollView
。 下面是我的问题:
- 我有超过10个选项卡,当我单击其中任何一个选项卡时,它应该将重心设置为中心? 我该如何解决这个问题。
TabHost
的示例应用程序,其中包含HorizontalScrollView
。 下面是我的问题:
int scrollX = (button.getLeft() - (screenWidth/2))+(button.getWidth()/2);
hScrollView.scrollTo(scrollX,0);
Where the
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.
但即使使用这种方法,您也无法将结束元素居中,因为滚动视图不会滚动超出其边界。
public void onTabChanged(String tabId) {
for (int i = 0; i < tablist.length; i++) {
if (tabId.contentEquals(tablist[i])) {
HorizontalScrollView scroll = (HorizontalScrollView)findViewById(R.id.horScrollView);
scroll.smoothScrollTo(((int)(getResources().getDimensionPixelSize(R.dimen.tab_width) * (i))), 0);
}
}
我使用了:implements OnTabChangeListener。