TabHost的使用

 TabActivity是一個過時的類,推薦使用Fragment.

實現選項卡的一般步驟:

1.在佈局文件中添加實現選項卡所需要的TabHost,TabWidget,和FrameLayout組件。

2.編寫各標籤頁中要顯示內容所對應的xml佈局文件。

3.在activity中,獲取並初始化TabHost組件。

4.爲TabHost對象添加標籤頁


xml文件:

<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/c92_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" >

            <AnalogClock
                android:id="@+id/c92_tab1"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_centerHorizontal="true" />

            <RelativeLayout
                android:id="@+id/rll"
                android:layout_width="match_parent"
                android:layout_height="match_parent" >

                <Button
                    android:id="@+id/c92_tab2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:onClick="onClick"
                    android:text="A semi-random Button" />

                <Button
                    android:id="@+id/c92_tab3"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_below="@id/c92_tab2"
                    android:text="A semi-random Button" />
            </RelativeLayout>
        </FrameLayout>
    </LinearLayout>

</TabHost>
在xml文件中添加選項卡組件時,必須使用系統的id,否則會報錯。


activity中:

public class MainActivity extends Activity {

	private TabHost tabs;
	
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.tabhost);
	
		
		// 步驟1:獲得TabHost的對象,並進行初始化setup()
		tabs = (TabHost) findViewById(R.id.c92_tabhost);
		tabs.setup();
		// 步驟2:通過TabHost.TabSpec增加tab的一頁,通過setContent()增加內容,通過setIndicator增加頁的標籤
		/* (1)增加第1頁 */
		TabHost.TabSpec spec; 
		spec = tabs.newTabSpec("Tag1");
		
		spec.setContent(R.id.c92_tab1);
		spec.setIndicator("Clock");
		tabs.addTab(spec);
		/* (2)增加第2頁 */
		spec = tabs.newTabSpec("Tag2");
		
		spec.setContent(R.id.rll);
		spec.setIndicator("Button");
		tabs.addTab(spec);
	
		// 步驟3:可通過setCurrentTab(index)指定顯示的頁,從0開始計算。
		tabs.setCurrentTab(1);
		
	}
	
	//動態添加一個Tab頁
	public void onClick(View view){
		TabHost.TabSpec spec = tabs.newTabSpec("tag3");
        	spec.setContent(new TabHost.TabContentFactory() {
            	/*createTabContent將返回View,這裏我們簡單用一個模擬時鐘*/
            	public View createTabContent(String tag) {
              	  return new AnalogClock(MainActivity.this);
            	}
        	});
       	 spec.setIndicator("other");
       	 tabs.addTab(spec);
	}
	
	
}
如上button監聽中,可以動態地添加一個tab標籤


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