天天看點

關于TabHost的一個例子

private int myMenuRes[] = {

R.drawable.tab1,

R.drawable.tab2,

R.drawable.tab3,

R.drawable.tab4,

R.drawable.tab5

};

TabHost tabHost;

TabSpec firstTabSpec;

TabSpec secondTabSpec;

TabSpec threeTabSpec;

TabSpec fourTabSpec;

TabSpec fiveTabSpec;

其中tab1,tab2,tab3,tab4,tab5都是drawable檔案下面的xml檔案。

tabHost = (TabHost) findViewById(android.R.id.tabhost);

tabHost.setBackgroundResource(R.drawable.nav_background);

firstTabSpec = tabHost.newTabSpec("tid1");

secondTabSpec = tabHost.newTabSpec("tid2");

threeTabSpec = tabHost.newTabSpec("tid3");

fourTabSpec = tabHost.newTabSpec("tid4");

fiveTabSpec = tabHost.newTabSpec("tid5");

firstTabSpec.setIndicator("Latest", getResources().getDrawable(

myMenuRes[0]));

secondTabSpec.setIndicator("Topics", getResources().getDrawable(

myMenuRes[1]));

threeTabSpec.setIndicator("Video", getResources().getDrawable(

myMenuRes[2]));

fourTabSpec.setIndicator("Podcast", getResources().getDrawable(

myMenuRes[3]));

fiveTabSpec.setIndicator("Gallery", getResources().getDrawable(

myMenuRes[4]));

firstTabSpec.setContent(new Intent(this, FirstTab.class));

secondTabSpec.setContent(new Intent(this, SecondTab.class));

threeTabSpec.setContent(new Intent(this, FirstTab.class));

fourTabSpec.setContent(new Intent(this, SecondTab.class));

fiveTabSpec.setContent(new Intent(this, SecondTab.class));

tabHost.addTab(firstTabSpec);

tabHost.addTab(secondTabSpec);

tabHost.addTab(threeTabSpec);

tabHost.addTab(fourTabSpec);

tabHost.addTab(fiveTabSpec);

點選有關的tab item下面的内容就會跳轉到setContent指定的Activity。

當要使用tabHost的時候可以參考此代碼,可以自己修改代碼來得到需要的tab item的個數,以及其顯示的内容,Activity裡面寫自己需要做的事情。還蠻不錯的哈。