我正在开发一个示例应用程序TabHost
,其中包含HorizontalScrollView
. 这是我的问题:
- 我有超过 10 个选项卡,当我单击任何选项卡时,它应该将重力设置为中心吗?我怎么能解决这个问题。
提前致谢!!!
我正在开发一个示例应用程序TabHost
,其中包含HorizontalScrollView
. 这是我的问题:
提前致谢!!!
这将涉及一些计算。
基本上,您将需要滚动到使元素居中的位置。
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.
但即使使用这种方法,您也无法将最终元素居中,因为滚动视图不会滚动超出其边界。
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。