底部菜单栏FragmentTabHost+Fragment

首先是底部菜单栏的布局




    

    

        

    



        Tab tab_home=new Tab(R.string.home,R.drawable.selector_icon_home,HomeFragment.class);
        Tab tab_hot=new Tab(R.string.hot,R.drawable.selector_icon_hot,HotFragment.class);
        Tab tab_category=new Tab(R.string.catagory,R.drawable.selector_icon_category, CategoryFragment.class);
        Tab tab_cart=new Tab(R.string.cart,R.drawable.selector_icon_cart,CartFragment.class);
        Tab tab_mine=new Tab(R.string.mine,R.drawable.selector_icon_mine,MineFragment.class);
        mTabs.add(tab_home);
        mTabs.add(tab_hot);
        mTabs.add(tab_category);
        mTabs.add(tab_cart);
        mTabs.add(tab_mine);
        mInflater=LayoutInflater.from(this);
        mTabHost= (FragmentTabHost) findViewById(android.R.id.tabhost);
        mTabHost.setup(this,getSupportFragmentManager(),R.id.realtabcontent);
        for (Tab tab:mTabs){
            TabHost.TabSpec tabSpec=mTabHost.newTabSpec(getString(tab.getTitle()));
            View  view=buildindicator(tab);
            tabSpec.setIndicator(view);
            mTabHost.addTab(tabSpec,tab.getFragment(),null);
        }
        mTabHost.getTabWidget().setShowDividers(LinearLayout.SHOW_DIVIDER_NONE);
        mTabHost.setCurrentTab(0);

    }
    private View buildindicator(Tab tab){
        View view=mInflater.inflate(R.layout.tab_indicator,null);
        ImageView img= (ImageView) view.findViewById(R.id.icon_tab);
        TextView text= (TextView)view.findViewById(R.id.txt_indicator);
        img.setBackgroundResource(tab.getIcon());
        text.setText(tab.getTitle());
        return view;
    }

你可能感兴趣的:(底部菜单栏FragmentTabHost+Fragment)