机器人TabHost中心活动标签 [英] Android TabHost Center active Tab

查看:151
本文介绍了机器人TabHost中心活动标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能改变Android的标签,如果你preSS之一,它涉及到TabHost的中心。只有3个选项卡是可见的,不要紧,有多少。像在Android 4.0谷歌音乐应用程序! 如果是,你会怎么做呢?

Is it possible to change Android Tabs that if you press one, it comes to the center of the TabHost. Only 3 Tabs are visible, doesn't matter how many there are. Like in the Android 4.0 Google Music App! If it is, how would you do that?

感谢您!

推荐答案

我没有找到自己的答案
如果是其他人需要它,这应该这样做:

I did find the answer myself.
If somebody else needs it, this should do it:

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

这篇关于机器人TabHost中心活动标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆