我正在开发一个TabHost
的示例应用程序,其中包含HorizontalScrollView
。我的问题是:
- 我有超过10个标签,当我点击任何标签,它应该设置为中心的重力?我怎样才能克服这个问题呢?
提前感谢!!
这将涉及一些计算。
实际上你需要滚动到元素居中的位置。
int scrollX = (button.getLeft() - (screenWidth/2))+(button.getWidth()/2);
hScrollView.scrollTo(scrollX,0);
其中
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.
但是即使使用这个方法,你也不能将结束元素居中,因为scrollviews不会滚动超过其边界。
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);
}
}
我使用:实现OnTabChangeListener