1

我有一个显示 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);
            }
        }
    }           

}

}

4

0 回答 0