tabhost 背景设置

阅读更多

 

话不多说,直接看代码吧!

 

 

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);     
        setContentView(R.layout.main);    
        Resources res = getResources(); 
        final TabHost tabHost = getTabHost();  // The activity TabHost
        TabHost.TabSpec spec;  
        Intent intent;  // Reusable Intent for each tab        
        intent = new Intent().setClass(this, IndexActivity.class);
        spec = tabHost.newTabSpec("appInfo").setIndicator("",
                res.getDrawable(R.drawable.ic_tab_home))
                      .setContent(intent);
        tabHost.addTab(spec);
        intent = new Intent().setClass(this, TypeActivity.class);
        spec = tabHost.newTabSpec("type").setIndicator("",
                          res.getDrawable(R.drawable.ic_tab_type))
                      .setContent(intent);
        tabHost.addTab(spec);
        intent = new Intent().setClass(this, RankActivity.class);
        spec = tabHost.newTabSpec("rank").setIndicator("",
                res.getDrawable(R.drawable.ic_tab_rank))
            .setContent(intent);
        tabHost.addTab(spec);
        
        intent = new Intent().setClass(this, SearchActivity.class);
        spec = tabHost.newTabSpec("search").setIndicator("",
                res.getDrawable(R.drawable.ic_tab_search))
                .setContent(intent);
        tabHost.addTab(spec);
        
        intent = new Intent().setClass(this, ManageActivity.class);
        spec = tabHost.newTabSpec("manage").setIndicator("",
                res.getDrawable(R.drawable.ic_tab_manage))
                .setContent(intent);
        tabHost.addTab(spec);

        tabHost.setCurrentTab(0);
        
        View view ;
        tabWidget=tabHost.getTabWidget();
        for (int i = 0; i < tabWidget.getChildCount(); i++) {
//            获取tabview项
            view=tabWidget.getChildAt(i);
            view.setBackgroundResource(R.drawable.tab_normal); 
//            选择项处理
            if (tabHost.getCurrentTab()==i) {
                view.setBackgroundResource(R.drawable.tab_selected);  
            }
//            tabchanged的监听
            tabHost.setOnTabChangedListener(new OnTabChangeListener() {
                
                @Override
                public void onTabChanged(String tabId) {
                    // TODO Auto-generated method stub
                    for (int j = 0; j < tabHost.getTabWidget().getChildCount(); j++) {
                        View view=tabHost.getTabWidget().getChildAt(j);
                           // 设置tab背景颜色   
                        view.setBackgroundResource(R.drawable.tab_normal); 
                        // 选中的进行处理   
                        if (tabHost.getCurrentTab() == j) {  
                            view.setBackgroundResource(R.drawable.tab_selected);  
                        } 
                    }
                    
                }
            });
        }    
    }

 

你可能感兴趣的:(Windows,Mobile,Apple,java,android)