概述
相信使用過android手機的朋友都見過下面樣子的選項卡窖铡,本文我們嘗試做看看溉委。
思路
這個選項卡頁面点待,或者說是標簽卡奏路。分為兩部分:
- 一個頂部的按鈕(可點擊的)的切換卡部分
- 一個主內(nèi)容區(qū)(上圖顯示“第二個窗體”字體的)的主顯示區(qū)畴椰。
實現(xiàn)
我們想實現(xiàn)的效果是點擊切換的選項卡卡部分,主顯示區(qū)的內(nèi)容隨之改變鸽粉。那么我們看下頁面布局代碼
<TabHost
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TabWidget android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingLeft="1dip"
android:paddingRight="1dip"
android:paddingTop="4dip"
/>
<FrameLayout android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="1dip"
android:layout_weight="1"
/>
</LinearLayout>
</TabHost>
如上面的代碼所示:
整個窗體在一個TabHost元素下斜脂。TabHost是根元素。他包含了一個子布局對象LinearLayout触机,這個布局對象的方向為“vertical”帚戳,注意這個垂直方向。它指示了如何排列這個布局對象的子對象儡首,也就是它指示了TabWidget 和FrameLayout 這兩個控件的排列片任。我們目前的排列是 TabWidget 在上,F(xiàn)rameLayout 在下蔬胯。如果想實現(xiàn)“選項卡標簽在底部的效果”对供,嘗試下relativeLayout吧。
TabWidget 就是標簽卡對象笔宿。就是用來切換的那個頂部標簽卡犁钟。注意id必須為@android:id/tabs
FrameLayout 是內(nèi)容區(qū)域,當標簽卡變化時泼橘,這里的內(nèi)容會隨之變化涝动。注意id必須為@android:id/tabcontent
布局構(gòu)建完畢后。下一步要做的炬灭,就是如何為這個布局添加子選項卡了醋粟。
首先,讓我們的activty繼承自TabActivity
public class ActTabActivityDemo1 extends TabActivity
在onCreate時獲得tabHost對象,并添加選項卡
_tabHost = getTabHost();
AddTabPage1(); //執(zhí)行添加子選項卡的方法
我們看下AddTabPage1()方法的具體實現(xiàn)米愿。
Intent internt1 = new Intent();
internt1.setClass(this,Act1.class);
TabSpec tabSpec = _tabHost.newTabSpec("act1");
//指定選項卡的顯示名稱
tabSpec.setIndicator("選項卡一");
//指定跳轉(zhuǎn)方向
tabSpec.setContent(internt1);
_tabHost.addTab(tabSpec);
如上面的代碼所示厦凤,我們構(gòu)建了一個TabSpec 對象,并調(diào)用_tabHost.addTab(tabSpec);方法育苟,將這個對象加入到選項卡集合中绿鸣。
TabSpec 是一個 選項卡對象反番,或者說是 TabSpec 描述一個選項卡的缘揪。通過
tabSpec.setIndicator 指定選項卡的顯示名稱证逻。
tabSpec.setContent(internt1); 指定跳轉(zhuǎn)方向,在這里指定了當頂部的標簽卡被指定時漱竖,執(zhí)行的Intent 對象禽篱。我們常常使用Intent 來做窗體間的跳轉(zhuǎn)。
完成了上述步驟后馍惹,就可以具體實現(xiàn) 具體的選項卡 里的布局(內(nèi)容)了躺率。
下面貼出完成的Activity代碼
package demo.TabActivityDemo1;
import android.app.Activity;
import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
public class ActTabActivityDemo1 extends TabActivity {
private TabHost _tabHost;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
_tabHost = getTabHost();
AddTabPage1();
AddTabPage2();
AddTabPage1();
}
private void AddTabPage1() {
// TODO Auto-generated method stub
Intent internt1 = new Intent();
internt1.setClass(this,Act1.class);
TabSpec tabSpec = _tabHost.newTabSpec("act1");
//指定選項卡的顯示名稱
tabSpec.setIndicator("選項卡一");
//指定跳轉(zhuǎn)方向
tabSpec.setContent(internt1);
_tabHost.addTab(tabSpec);
}
private void AddTabPage2() {
// TODO Auto-generated method stub
Intent internt1 = new Intent();
internt1.setClass(this,Act2.class);
TabSpec tabSpec = _tabHost.newTabSpec("act2");
tabSpec.setIndicator("選項卡二");
tabSpec.setContent(internt1);
_tabHost.addTab(tabSpec);
}
}