android tabhost学习

public class TabWidgetActivity extends TabActivity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
    	super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		Resources res = getResources(); // Resource object to get 
		TabHost tabHost = getTabHost(); // The activity TabHost
		TabHost.TabSpec spec; //  TabSpec for each tab
		Intent intent; // Reusable Intent for each tab
		// Create an Intent to launch an Activity for the tab (to be reused)
		intent = new Intent().setClass(this, ArtistsActivity.class);
		//新建一个TabSpec  tabHost.newTabSpec(tag名).setIndicator("显示的名字", "图片").setContent(设置到intent);
		spec = tabHost.newTabSpec("artists").setIndicator("Artists", res.getDrawable(R.drawable.ic_tab_artists)).setContent(intent);
		tabHost.addTab(spec);//添加到TabHost
		// Do the same for the other tabs
		intent = new Intent().setClass(this, AlbumsActivity.class);
		
		spec = tabHost.newTabSpec("albums").setIndicator("Albums", res.getDrawable(R.drawable.ic_tab_albums)).setContent(intent);
		tabHost.addTab(spec);

		intent = new Intent().setClass(this, SongsActivity.class);
		spec = tabHost.newTabSpec("songs").setIndicator("Songs", res.getDrawable(R.drawable.ic_tab_songs)).setContent(intent);
		tabHost.addTab(spec);

		tabHost.setCurrentTab(2);//当前显示哪个tab
       }
    }

 

你可能感兴趣的:(android)