Android TabHost的学习笔记

来源:互联网 发布:windows xp sp3原版 编辑:程序博客网 时间:2024/05/17 23:30
  1. package cn.com.karl.music;  
  2.   
  3. import android.app.TabActivity;  
  4. import android.content.Intent;  
  5. import android.content.res.Resources;  
  6. import android.os.Bundle;  
  7. import android.view.Window;  
  8. import android.view.WindowManager;  
  9. import android.widget.TabHost;  
  10.   

  11. public class MainActivity extends TabActivity {  
  12.     /** Called when the activity is first created. */  
  13.     @Override  
  14.     public void onCreate(Bundle savedInstanceState) {  
  15.         super.onCreate(savedInstanceState);  
  16.         requestWindowFeature(Window.FEATURE_NO_TITLE);
  17.         this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,  
  18.                 WindowManager.LayoutParams.FLAG_FULLSCREEN);
  19.         setContentView(R.layout.main);
  20.   
  21.         Resources res = getResources();
  22.        
  23.         TabHost tabHost = getTabHost(); 
  24.         TabHost.TabSpec spec;  
  25.         Intent intent;  
  26.         intent = new Intent().setClass(this, ListActivity.class); 
  27.          
  28.         spec = tabHost.newTabSpec("music")  
  29.                 .setIndicator("music", res.getDrawable(R.drawable.item))  
  30.                 .setContent(intent);  
  31.         tabHost.addTab(spec);  
  32.   
  33.         intent = new Intent().setClass(this, ArtistsActivity.class);  
  34.         spec = tabHost.newTabSpec("artist")  
  35.                 .setIndicator("srtist", res.getDrawable(R.drawable.artist))  
  36.                 .setContent(intent);  
  37.         tabHost.addTab(spec);  
  38.   
  39.         intent = new Intent().setClass(this, AlbumsActivity.class);  
  40.         spec = tabHost.newTabSpec("special")  
  41.                 .setIndicator("special", res.getDrawable(R.drawable.album))  
  42.                 .setContent(intent);  
  43.         tabHost.addTab(spec);  
  44.         intent = new Intent().setClass(this, SongsActivity.class);  
  45.         spec = tabHost.newTabSpec("recent play")  
  46.                 .setIndicator("recent play"res.getDrawable(R.drawable.album))  
  47.                 .setContent(intent);  
  48.         tabHost.addTab(spec);  
  49.   
  50.         
  51.         tabHost.setCurrentTab(0);  
  52.   
  53.     }  
  54. }  
页面布局:
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <TabHost xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:id="@android:id/tabhost"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent" >  
  6.   
  7.     <LinearLayout  
  8.         android:layout_width="fill_parent"  
  9.         android:layout_height="fill_parent"  
  10.         android:orientation="vertical"  
  11.         android:padding="5dp" >  
  12.   
  13.         <TabWidget  
  14.             android:id="@android:id/tabs"  
  15.             android:layout_width="fill_parent"  
  16.             android:layout_height="wrap_content" />  
  17.   
  18.         <FrameLayout  
  19.             android:id="@android:id/tabcontent"  
  20.             android:layout_width="fill_parent"  
  21.             android:layout_height="fill_parent"  
  22.             android:padding="5dp" />  
  23.     </LinearLayout>  
  24.   
  25. </TabHost>