7.1.5 選項卡結合案例詳解
選項卡是通過TabHost和TabActivity一起實現的,TabHost是Android中很常用的布局之一,它的標簽可以有文本和文本圖片樣式。點擊不同標簽還可以切換標簽。TabHost類的繼承圖如下:java.lang.Object
↳android.view.View
↳android.view.ViewGroup
↳android.widget.FrameLayout
↳android.widget.TabHost
android.widget.TabHost繼承了android.widget.FrameLayout框架布局類。下麵是一個文本圖片選項卡例子,如圖7-10所示。

圖7-10 TabHost1
代碼請參考代碼清單7-11,完整代碼請參考chapter7_1工程中Tab1代碼部分。
【代碼清單7-11】
public class Tab1 extends TabActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TabHost tabHost = getTabHost();
LayoutInflater.from(this).inflate(R.layout.tab1_layout,
tabHost.getTabContentView(), true);
tabHost.addTab(tabHost.newTabSpec("tab1").setIndicator("tab1",
getResources().getDrawable(R.drawable.redimage)).setContent(
R.id.view1));
tabHost.addTab(tabHost.newTabSpec("tab3").setIndicator("tab2",
getResources().getDrawable(R.drawable.yellowimage)).setContent(
R.id.view2));
tabHost.addTab(tabHost.newTabSpec("tab3").setIndicator("tab3")
.setContent(R.id.view3));
}
}
選項卡屏幕的Activity必須繼承TabActivity,通過基類TabActivity提供的方法getTabHost()可以獲得TabHost對象。下麵的代碼實現了為TabHost指定布局文件:
LayoutInflater.from(this).inflate(R.layout.tab1_layout,
tabHost.getTabContentView(), true);
addTab(TabHost.TabSpec tabSpec)方法可以添加選項卡的標簽,本例中有三個標簽。TabHost.TabSpec調用setIndicator()設置標簽樣式,有三個setIndicator ()方法:
• setIndicator(CharSequence label) 指定標簽的文本信息;
• setIndicator(CharSequence label, Drawable icon) 指定文本圖片標簽;
• setIndicator(View view) 使用一個View指定標簽。
TabHost.TabSpec調用setContent ()設置各個選項卡容納的內容,有三個setContent ()方法:
• setContent(TabHost.TabContentFactory contentFactory) 通過TabHost.TabContentFactory工廠類創建選項卡的內容;
• setContent(int viewId) 通過一個id指定選項卡內容;
• setContent(Intent intent) 通過一個Intent指定選擇選項卡跳轉到一個Activity。
布局文件請參考代碼清單7-12,完整代碼請參考chapter7_1工程中tab1_layout.xml代碼部分(chapter7_1/res/layout/tab1_layout.xml)。
【代碼清單7-12】
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:andro
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView android:
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/tab1"/>
<TextView android:
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/tab2"/>
<TextView android:
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/tab3"/>
</LinearLayout>
下麵的例子是指定一個Intent的選項卡跳轉到一個Activity的例子,如圖7-11所示,有兩個選項卡:城市列表和城市展示。

圖7-11 TabHost2
代碼請參考代碼清單7-13,完整代碼請參考chapter7_1工程中Tab2代碼部分。
【代碼清單7-13】
public class Tab2 extends TabActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TabHost tabHost = getTabHost();
tabHost.addTab(tabHost.newTabSpec("tab1").setIndicator("城市列表")
.setContent(new Intent(this, ListView_1.class)));
tabHost.addTab(tabHost.newTabSpec("tab3").setIndicator("城市展示")
.setContent(
new Intent(this, ListViewIcon_3.class)
.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)));
}
}
通過setContent方法設置Intent,當用戶點擊了選項卡的標簽後,選項卡的內容會跳轉。跳轉之後的ListView_1和ListViewIcon_3是兩個ListView展示城市信息,這兩個ListView在第6章已經介紹了,這裏就不再介紹了。
出自《Android開發案例驅動教程》第七章
最後更新:2017-04-02 06:51:48