tabhost笔记

不推荐使用

 

    TabHost mTabHost;
    View mTabHomeView, mTabBookView, mTabSpaceView, mTabMeView;
    ImageView mTabImage;
    TextView mTabTitle;
    Intent mIntent;
    public void initTab(Bundle savedInstanceState) {
        // 获取TabHost对象
        mTabHost = (TabHost) findViewById(R.id.tabhost);
        // 如果没有继承TabActivity时,通过该种方法加载启动tabHost
        //mTabHost.setup();
        LocalActivityManager mLocalActivityManager = new LocalActivityManager(this, false);
        mLocalActivityManager.dispatchCreate(savedInstanceState);
        mTabHost.setup(mLocalActivityManager);



        mTabHomeView = LayoutInflater.from(this).inflate(R.layout.tab_cell_home, null);
        mTabBookView = LayoutInflater.from(this).inflate(R.layout.tab_cell_book, null);
        mTabSpaceView = LayoutInflater.from(this).inflate(R.layout.tab_cell_space, null);
        mTabMeView = LayoutInflater.from(this).inflate(R.layout.tab_cell_me, null);

        this.addTab(mTabHomeView, mRes.getDrawable(R.drawable.tab_bg), R.string.tab_title_home);
        this.addTab(mTabBookView, mRes.getDrawable(R.drawable.tab_bg), R.string.tab_title_book);
        this.addTab(mTabSpaceView, mRes.getDrawable(R.drawable.tab_bg), R.string.tab_title_space);
        this.addTab(mTabMeView, mRes.getDrawable(R.drawable.tab_bg), R.string.tab_title_me);

        mTabHost.setOnTabChangedListener(new TabHost.OnTabChangeListener() {
            @Override
            public void onTabChanged(String s) {
                mActionTitle.setText(s);
            }
        });
        mTabHost.setCurrentTab(0);
        mActionTitle.setText(mRes.getString(R.string.tab_title_home));
    }

    public void addTab(View tabview, Drawable picture, int stringId) {
        String title = mRes.getString(stringId);
        switch (stringId) {
            case R.string.tab_title_home:
                mIntent = new Intent(this, HomeActivity.class);
                break;
            case R.string.tab_title_book:
                mIntent = new Intent(this, BookActivity.class);
                break;
            case R.string.tab_title_space:
                mIntent = new Intent(this, SpaceActivity.class);
                break;
            case R.string.tab_title_me:
                mIntent = new Intent(this, MeActivity.class);
                break;
            default:
                break;
        }
        mTabImage  = (ImageView)tabview.findViewById(R.id.tab_picture);
        mTabTitle = (TextView)tabview.findViewById(R.id.tab_title);
        mTabImage.setImageDrawable(picture);
        mTabTitle.setText(title);

        mTabHost.addTab(mTabHost.newTabSpec(title)
                .setIndicator(tabview)
                .setContent(mIntent));
    }

 

posted @ 2015-10-22 14:25  williamgufeng  阅读(186)  评论(0编辑  收藏  举报