android tab test

// mTabHost定义在Activity的属性
        mTabHost = (TabHost) findViewById(R.id.tabhost);
        mTabHost.setup();

        LayoutInflater factory = null;
        // 未开始
        factory = LayoutInflater.from(this);
        final View notStartview = factory.inflate(R.layout.tab,null);
        TextView notStartTextView = (TextView)notStartview.findViewById(R.id.tabName);
                notStartTextView.setText(getText(R.string.text_tab_notstart));
                notStartTextView
               .setBackgroundDrawable(getResources().getDrawable(R.drawable.tab));

        // 进行中
        factory = LayoutInflater.from(this);
        final View playingView = factory.inflate(R.layout.tab,null);
        TextView playingTextView = (TextView)playingView.findViewById(R.id.tabName);
        playingTextView.setText(getText(R.string.text_tab_playing));
        playingTextView.setBackgroundDrawable(getResources().getDrawable(R.drawable.tab_act));

        // 已完场
        factory = LayoutInflater.from(this);
        final View finishView = factory.inflate(R.layout.tab,null);
        TextView finishTextView = (TextView)finishView.findViewById(R.id.tabName);
        finishTextView.setText(getText(R.string.text_tab_finish));
        finishTextView.setBackgroundDrawable(getResources().getDrawable(R.drawable.tab));

        // 关注
        factory = LayoutInflater.from(this);
        final View focusView = factory.inflate(R.layout.tab,null);
        TextView focusTextView = (TextView)focusView.findViewById(R.id.tabName);
        focusTextView.setText(getText(R.string.text_tab_focus));
        focusTextView.setBackgroundDrawable(getResources().getDrawable(R.drawable.tab));

        // 未开始
        mTabHost.addTab(mTabHost.newTabSpec("tab_not_begin")
          .setIndicator(notStartview)
          .setContent(R.id.textview_notbegin));
        // 进行中
        mTabHost.addTab(mTabHost.newTabSpec("tab_playing")
          .setIndicator(playingView)
          .setContent(R.id.textview_playing)); 
        // 已完场
        mTabHost.addTab(mTabHost.newTabSpec("tab_finish")
          .setIndicator(finishView)
          .setContent(R.id.textview_finish)); 
        // 关注
        mTabHost.addTab(mTabHost.newTabSpec("tab_focus")
          .setIndicator(focusView)
          .setContent(R.id.textview_focus)); 

        // 修改宽度,作用两个(关注)与三个字(进行中,已完场)占有不同,三个字宽度大一些
        for (int i =0; i < mTabHost.getTabWidget().getChildCount(); i++) {
                mTabHost.getTabWidget().getChildAt(i).getLayoutParams().height = DisplayUtil.dip2px(this, 33);

                if(i == 1 || i== 2){
                        mTabHost.getTabWidget().getChildAt(i).getLayoutParams().width = 18;
                } else{
                        mTabHost.getTabWidget().getChildAt(i).getLayoutParams().width = 10;
                }
        }

        // 设置当前选中的Tab
        mTabHost.setCurrentTab(1);
        mTabHost.getTabWidget().getChildAt(mTabHost.getCurrentTab()).setBackgroundColor(Color.BLACK);

        // tab 选中改变时事件
        mTabHost.setOnTabChangedListener(new TabHost.OnTabChangeListener() {
                public void onTabChanged(String arg0) {
                        for(int i=0;i<mTabHost.getTabWidget().getChildCount();i++){
                               mTabHost.getTabWidget().getChildAt(i).findViewById(R.id.tabName)
                                .setBackgroundDrawable(getResources().getDrawable(R.drawable.tab));
                        }
                        mTabHost.getTabWidget().getChildAt(mTabHost.getCurrentTab())
                        .findViewById(R.id.tabName)
                        .setBackgroundDrawable(getResources().getDrawable(R.drawable.tab_act));
                }
        });

你可能感兴趣的:(android)