天天看點

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标簽

繼續閱讀