Professional Documents
Culture Documents
Project : TabBarDemo
สําหรับโปรเจ็กนี้ จะแนะนําให้รู้จักกับ TabHost โดยที่เราจะใส่ TextView ไว้ ใน Tab แรก ส่วน Tab ที่
สอง เป็น Clock
<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
</TabWidget>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:id = "@+id/tab1"
android:layout_width = "fill_parent"
android:layout_height = "fill_parent"
android:orientation = "vertical"
android:gravity = "center" >
<TextView
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:text = "This is Tab1"
android:textSize = "18pt" >
</TextView>
</LinearLayout>
<LinearLayout
android:id = "@+id/tab2"
android:layout_width = "fill_parent"
android:layout_height = "fill_parent"
android:orientation = "vertical"
android:gravity = "center" >
<AnalogClock
android:layout_width = "wrap_content"
android:layout_height = "wrap_content" >
</AnalogClock>
</LinearLayout>
</FrameLayout>
</TabHost>
<TabHost
android:id="@+id/tabHost"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android" >
<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
</TabWidget>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:id = "@+id/tab2"
android:layout_width = "fill_parent"
android:layout_height = "fill_parent"
android:orientation = "vertical"
android:gravity = "center" >
<AnalogClock
android:layout_width = "wrap_content"
android:layout_height = "wrap_content" >
</AnalogClock>
</LinearLayout>
tabBar = (TabHost)findViewById(R.id.tabHost);
tabBar.setup();
spec = tabBar.newTabSpec("Tab1");
spec.setContent(R.id.tab1);
spec.setIndicator("Tab1");
tabBar.addTab(spec);
spec = tabBar.newTabSpec("Tab2");
spec.setContent(R.id.tab2);
spec.setIndicator("Clock");
tabBar.addTab(spec);
}
}
TabHost tabBar;
TabHost.TabSpec spec;
tabBar = (TabHost)findViewById(R.id.tabHost);
tabBar.setup();
spec = tabBar.newTabSpec("Tab1");
2. ใส่ข้อมูลใน Tab นั้นจาก id ที่สร้างไว้สําหรับ Tab แรก ในที่นี้คือ
LinearLayout ที่มี่ชื่อ id ว่า tab1 จะได้ว่า
spec.setContent(R.id.tab1);
3. กําหนดชื่อ Tab ที่แสดงให้ User เห็น ด้วยฟังก์ชัน setIndicator จะได้ว่า
spec.setIndicator("Tab1");
4. Add TabSpace ลงไปใน TabHost ด้วยฟังก์ชัน AddTab จะได้ว่า
tabBar.addTab(spec);
spec = tabBar.newTabSpec("Tab2");
spec.setContent(R.id.tab2);
spec.setIndicator("Clock");
tabBar.addTab(spec);
drbomkung@gmail.com