我有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);
}
请告诉我可能出现的问题..
答案 0 :(得分: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);
}