選項卡之 ——從佈局文件中獲得TabHost

//切記不要使用大圖片
public class Tab1Activity extends Activity {

	
	private TabHost tabHost;  //準備TabHost 管理

	 @Override
	 protected void onCreate(Bundle savedInstanceState) {
	 super.onCreate(savedInstanceState);
	 setContentView(R.layout.activity_tab1);
	 
	 tabHost = (TabHost)findViewById(R.id.TabHost);  //tabhost
	 tabHost.setup();  //一定要寫這步驟
	 
	 addTab();//初始化選項卡
	 
	 // 設置TabHost背景顏色
	 tabHost.setBackgroundColor(Color.argb(150, 20, 80, 150));
	 // 設置TabHost背景圖片資源
	 tabHost.setBackgroundResource(R.drawable.ic_launcher);  
	 // 設置當前顯示哪一個標籤 
	 tabHost.setCurrentTab(0);
	 // 標籤切換事件處理,setOnTabChangedListener 標籤切換事件
	 tabHost.setOnTabChangedListener(new OnTabChangeListener() {
		@Override
		public void onTabChanged(String tabId) {	
			Toast.makeText(Tab1Activity.this, "tabid:"+tabId, 0).show();
		}	 
	 });
	 }

	    @Override
		public boolean onCreateOptionsMenu(Menu menu) {
			// Inflate the menu; this adds items to the action bar if it is present.
			getMenuInflater().inflate(R.menu.tab1, menu);
			return true;
		}

	 
	 // 爲TabHost添加標籤 新建一個newTabSped(new TabSpec) 設置其標籤和圖標(setIndicator)、設置內容(setContent)
	 private void addTab() {
       tabHost.addTab(tabHost.newTabSpec("java").setIndicator("Java之路",
	   getResources().getDrawable(R.drawable.ic_launcher))
	   .setContent(R.id.javaWorker));
       
	   // 指定內容爲一個TextView --->public TabHost.TabSpec setContent(int viewId) 此方法需要一個 viewId 作爲參數
       tabHost.addTab(tabHost.newTabSpec("android").setIndicator("Android之路",
    		   getResources().getDrawable(R.drawable.ic_launcher))
    		   .setContent(R.id.androidWorker));
       
       tabHost.addTab(tabHost.newTabSpec("math").setIndicator("算法之路",
    		   getResources().getDrawable(R.drawable.ic_launcher))
    		   .setContent(R.id.systemWorker));
	}
}

xml佈局如下:

<TabHost xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/TabHost"
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" >
  <LinearLayout 
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
    <TabWidget
        android:id="@android:id/tabs"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />
    <FrameLayout
    android:id="@android:id/tabcontent"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
    
        <TextView android:id="@+id/javaWorker"
         android:layout_width="match_parent"
         android:layout_height="match_parent"
         android:text="java工程師" 
         android:textColor="#FF0000"/>
        <TextView android:id="@+id/androidWorker"
         android:layout_width="match_parent"
         android:layout_height="match_parent"
         android:text="android工程師" 
         android:textColor="#385E0F"/>
        <TextView android:id="@+id/systemWorker"
         android:layout_width="match_parent"
         android:layout_height="match_parent"
         android:text="系統分析師" 
         android:textColor="#1E90FF"/>

    </FrameLayout>
  </LinearLayout>
</TabHost>


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章