每个活动上的标签栏

时间:2012-09-26 15:34:14

标签: android android-tabactivity

我有一个显示5个标签的标签栏。我希望标签栏显示在我的应用中的每个活动中。标签栏上显示的唯一活动是选项卡中的5个活动。我正在使用TabActivity,我知道它已被弃用,但除了这5个活动之外,我有什么方法可以让它在任何地方工作吗?我需要一个相当快速的解决方案,这不会真正影响我的其他代码。

这是标签栏活动

package com.appname;

import android.app.TabActivity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TabWidget;

import com.readystatesoftware.viewbadger.BadgeView;

@SuppressWarnings("deprecation")
public class TabController extends TabActivity {  
   /** Called when the activity is first created. */ 
   TabHost tabHost; 
   SharedPreferences prefs;
   public static BadgeView badgeTab;
       @Override  
       public void onCreate(Bundle savedInstanceState) {  
            super.onCreate(savedInstanceState);  
            setContentView(R.layout.main);    

            tabHost = getTabHost();
            TabHost.TabSpec spec;  
            Intent intent;              

        intent = new Intent().setClass(this, Item1.class);  
        spec = tabHost.newTabSpec("Item 1").setIndicator("Item 1",  getResources().getDrawable(R.drawable.item1))  
                  .setContent(intent); 
        tabHost.addTab(spec);               

        intent = new Intent().setClass(this, Item2.class);  
        spec = tabHost.newTabSpec("Item 2").setIndicator("Item 2", getResources().getDrawable(R.drawable.item2))  
                  .setContent(intent);
        tabHost.addTab(spec);

        intent = new Intent().setClass(this, Item3.class);  
        spec = tabHost.newTabSpec("Item 3").setIndicator("Item 3", getResources().getDrawable(R.drawable.item3))  
                  .setContent(intent);     
        tabHost.addTab(spec);

        intent = new Intent().setClass(this, Item4.class);  
        spec = tabHost.newTabSpec("Item 4").setIndicator("Item 4", getResources().getDrawable(R.drawable.item4))  
                  .setContent(intent);              
        tabHost.addTab(spec);

        intent = new Intent().setClass(this, Item5.class);  
        spec = tabHost.newTabSpec("Item 5").setIndicator("Item 5", getResources().getDrawable(R.drawable.item5))  
                  .setContent(intent);      
        tabHost.addTab(spec);                  

        //Set background images
       for(int i=0;i<tabHost.getTabWidget().getChildCount();i++){                      

        tabHost.getTabWidget().getChildAt(i).setBackgroundResource(R.drawable.tabbackground); //unselected
       }
       tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab()).setBackgroundResource(R.drawable.selectedtab); // selected

        tabHost.setOnTabChangedListener(new OnTabChangeListener(){
        public void onTabChanged(String tabId) {
            for(int i=0;i<tabHost.getTabWidget().getChildCount();i++)
            {
                tabHost.getTabWidget().getChildAt(i).setBackgroundResource(R.drawable.tabbackground); //unselected
            }
            tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab()).setBackgroundResource(R.drawable.selectedtab); // selected
        }
    });

    TabWidget tabs = (TabWidget) findViewById(android.R.id.tabs);               
    badgeTab = new BadgeView(this, tabs, 2);
    badgeTab.setBadgePosition(2);
    if(NotificationService.messageCount <= 0){
        badgeTab.hide();
    }
    else{                                                                                                               
        badgeTab.setText(String.valueOf(NotificationService.messageCount));
        badgeTab.show();
    }

    Intent tabIntent = this.getIntent();
    if (tabIntent != null && tabIntent.getExtras() != null) {
        if(tabIntent.getExtras().containsKey("ID_KEY")){
            int id_Key = this.getIntent().getExtras().getInt("ID_KEY");

            if (id_Key > 0) {
                tabHost.setCurrentTab(2);
            }
        }
    }           

}

}

0 个答案:

没有答案