我有 6 个选项卡,当我单击任何选项卡时,它应该将重力设置为中心。我该怎么做..?
这是代码,但它没有效果。请帮忙..
Calendar c = Calendar.getInstance();
int day = c.get(Calendar.DAY_OF_WEEK);
try{
int screenWidth = getWindowManager().getDefaultDisplay().getWidth();
int scrollX = (th.getTabWidget().getChildAt(day-2).getLeft() - (screenWidth/2))
+(th.getTabWidget().getChildAt(day-2).getWidth()/2);
hsv.scrollTo(scrollX,0);
//hsv is horizontalScrollView from the xml file
//th is the tabhost
th.setCurrentTab(day-2);
}catch(Exception e){
th.setCurrentTab(2);
}
请告诉我可能出了什么问题。
试试这个。它有效
public void centerTabItem(int position) {
tabHost.setCurrentTab(position);
final TabWidget tabWidget = tabHost.getTabWidget();
final int screenWidth = getWindowManager().getDefaultDisplay().getWidth();
final int leftX = tabWidget.getChildAt(position).getLeft();
int newX = 0;
newX = leftX + (tabWidget.getChildAt(position).getWidth() / 2) - (screenWidth / 2);
if (newX < 0) {
newX = 0;
}
horizontalScrollView.scrollTo(newX, 0);
}