1

我有tabactivity5 个标签。每个都有自己的意图与类。现在我要做的是将相同的选项卡放在不包含在tabhosts tabspec. 我是Android的初学者。请多多包涵。

hometab.java:

public class HomeTab extends TabActivity {

private static final String LOG_TAG = null;
@SuppressWarnings("deprecation")
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
    setContentView(R.layout.activity_home_tab);
    getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.window_title);
    Resources ressources = getResources(); 

    final TabHost tabHost = getTabHost(); 
    Intent intentHome= new Intent().setClass(this, Home.class);
    TabSpec tabSpecHome = tabHost
      .newTabSpec("Home")
      .setIndicator("", ressources.getDrawable(R.drawable.home))
      .setContent(intentHome);

    Intent intentShopInfo= new Intent().setClass(this, ShopInfo.class);
    TabSpec tabSpecShopInfo = tabHost
      .newTabSpec("ShopInfo")
      .setIndicator("", ressources.getDrawable(R.drawable.home))
      .setContent(intentShopInfo);


    // Android tab  
    Intent intentDiscover= new Intent().setClass(this, MainAct.class);
    TabSpec tabSpecDiscover = tabHost
      .newTabSpec("MainAct")
      .setIndicator("", ressources.getDrawable(R.drawable.discover))
      .setContent(intentDiscover);

    // Apple tab
    Intent intentMode = new Intent().setClass(this, Mode.class);
    TabSpec tabSpecMode = tabHost
      .newTabSpec("Mode")
      .setIndicator("", ressources.getDrawable(R.drawable.mode))
      .setContent(intentMode);

    // Windows tab
    Intent intentExplore = new Intent().setClass(this, Explore.class);
    TabSpec tabSpecExplore = tabHost
      .newTabSpec("Explore")
      .setIndicator("", ressources.getDrawable(R.drawable.explore))
      .setContent(intentExplore);
    Intent intentSearch = new Intent().setClass(this, SearchView.class);
    TabSpec tabSpecSearch = tabHost
      .newTabSpec("SearchView")
      .setIndicator("", ressources.getDrawable(R.drawable.search))
      .setContent(intentSearch);

    // Blackberry tab


    // add all tabs 
    tabHost.addTab(tabSpecHome);
    tabHost.addTab(tabSpecDiscover);
    tabHost.addTab(tabSpecExplore);
    tabHost.addTab(tabSpecSearch);
    tabHost.addTab(tabSpecMode);

     for(int i=0;i<tabHost.getTabWidget().getChildCount();i++)
            tabHost.getTabWidget().getChildAt(i).setBackgroundColor(Color.parseColor("#eb8114")); //unselected


    tabHost.setOnTabChangedListener(new OnTabChangeListener() {
        @Override
        public void onTabChanged(String arg0) {         
             WifiManager wifi;
            wifi=(WifiManager)getSystemService(Context.WIFI_SERVICE);
            setTabColor(tabHost);
            ConnectionDetector cd = new ConnectionDetector(getApplicationContext());


            Boolean isInternetPresent = cd.isConnectingToInternet();

            //Checking whether  Wifi is on or off
            if(isInternetPresent)
            {

            }
            else
            {
                if(tabHost.getCurrentTab() == 1)
                {
                Toast.makeText(HomeTab.this,
                          "No Internet Connection Please Enable Wifi Mode to Enjoy Discover Service "  , Toast.LENGTH_LONG)
                          .show();
                tabHost.setCurrentTab(2);

                }

            }
        }       
    });  

    }
public void setTabColor(TabHost tabhost) {


    for(int i=0;i<tabhost.getTabWidget().getChildCount();i++)
        tabhost.getTabWidget().getChildAt(i).setBackgroundColor(Color.parseColor("#eb8114")); //unselected

    if(tabhost.getCurrentTab()==0)
    {
        getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.window_title);


    }
    else if(tabhost.getCurrentTab()==1)
    {

            getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.dis);
    }
    else if(tabhost.getCurrentTab()==2)
    {
        getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.exp);
    }
    else if(tabhost.getCurrentTab()==3)
    {
        getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.sea);
    }
    else if(tabhost.getCurrentTab()==4)
    {
        getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.mod);
    }
}

 public void Back(View v)
 {
    Toast.makeText(HomeTab.this,
          "No Internet Connection Please Enable Wifi Mode to Enjoy Discover Service "  , Toast.LENGTH_LONG)
          .show();
 }
 public void Next(View v)
 {
    Intent intent = new Intent(HomeTab.this,Dbase.class);
startActivity(intent);
 }

}

XML

    <FrameLayout
        android:id="@android:id/tabcontent"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_marginBottom="20dp"
        android:padding="5dp" />
          <TabWidget
        android:id="@android:id/tabs"

        android:layout_marginTop="20dp"
        android:layout_alignParentBottom="true"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        />
    </RelativeLayout>
</TabHost>
</RelativeLayout>

首先十分感谢。

4

1 回答 1

1

使用此参考。 然后使用ActivityGroup意图代码替换视图,如下所示:

Intent intent = new Intent(Events.this,EventInfo.class);
replaceContentView("activity3", intent);

使用的方法是:

private void replaceContentView(String id, Intent newIntent) {
        // TODO Auto-generated method stub
        View view = getLocalActivityManager().startActivity(id,
                newIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))
                .getDecorView();
        this.setContentView(view);
    }

这就是为我解决问题的原因。我希望这可以帮助很多人。

于 2013-11-20T19:54:20.947 回答