1

我有 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);
    }

请告诉我可能出了什么问题..

4

1 回答 1

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);
    }
于 2012-08-11T21:22:09.260 回答