【问题标题】:Android TabHost Center active TabAndroid TabHost 中心活动选项卡
【发布时间】:2012-03-26 19:53:51
【问题描述】:

是否可以更改 Android 选项卡,如果您按一个,它会到达 TabHost 的中心。只有 3 个选项卡可见,不管有多少。就像在 Android 4.0 谷歌音乐应用中一样! 如果是,你会怎么做?

谢谢!

【问题讨论】:

    标签: android center android-4.0-ice-cream-sandwich android-tabs


    【解决方案1】:

    我自己确实找到了答案。
    如果其他人需要它,应该这样做:

        final HorizontalScrollView mHorizontalScrollView;
        float scale = getResources().getDisplayMetrics().density;
        final double tabWidth = (int) (150 * scale + 0.5f);
    
        for (int i = 0; i < tabHost.getTabWidget().getTabCount(); i++) {
            tabHost.getTabWidget().getChildTabViewAt(i).getLayoutParams().width = (int) tabWidth;
        }
    
        final double screenWidth = getWindowManager().getDefaultDisplay()
                .getWidth();
        mHorizontalScrollView = (HorizontalScrollView) findViewById(R.id.hscrollview);
    
        tabHost.setOnTabChangedListener(new OnTabChangeListener() {
    
            @Override
            public void onTabChanged(String tabId) {
                int nrOfShownCompleteTabs = ((int) (Math.floor(screenWidth
                        / tabWidth) - 1) / 2) * 2;
                int remainingSpace = (int) ((screenWidth - tabWidth - (tabWidth * nrOfShownCompleteTabs)) / 2);
    
                int a = (int) (tabHost.getCurrentTab() * tabWidth);
                int b = (int) ((int) (nrOfShownCompleteTabs / 2) * tabWidth);
                int offset = (a - b) - remainingSpace;
    
                mHorizontalScrollView.scrollTo(offset, 0);
            }
        });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-05
      相关资源
      最近更新 更多