Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

TabHost實現頁面內多選項卡切換的完整流程

編輯:關於 Android 程式設計

TabHost 可以在同一個界面裡容納多個選項卡,很多應用都用它來做頂部或底部的分欄切換。本文從兩個基礎概念講起,再走一個可運行的最小範例。

一、基礎概念

TabWidget:就是 TabHost 上方或下方那一排可點擊的按鈕,負責觸發選項卡切換。
TabSpec:代表一個選項卡界面,newTabSpec(String tag) 建立,addTab(tabSpec) 加入 TabHost。

二、實例講解

TabHost 的基本用法有兩個要點:佈局中的 id 必須使用系統預定義值,Activity 則繼承 TabActivity。

main.xml

 <TabHost xmlns:android="https://schemas.android.com/apk/res/android"
     xmlns:tools="https://schemas.android.com/tools"
     android:id="@android:id/tabhost"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     tools:context=".Main" >

     <LinearLayout
         android:layout_width="match_parent"
         android:layout_height="match_parent"
         android:orientation="vertical" >

         <TabWidget
             android:id="@android:id/tabs"
             android:layout_width="match_parent"
             android:layout_height="wrap_content" >
         </TabWidget>

         <FrameLayout
             android:id="@android:id/tabcontent"
             android:layout_width="match_parent"
             android:layout_height="wrap_content" >

             <LinearLayout
                 android:id="@+id/tab1"
                 android:layout_width="match_parent"
                 android:layout_height="wrap_content"
                 android:orientation="vertical" >
                 <TextView
                     android:layout_width="match_parent"
                     android:layout_height="match_parent"
                     android:text="aa" />
             </LinearLayout>

             <LinearLayout
                 android:id="@+id/tab2"
                 android:layout_width="match_parent"
                 android:layout_height="wrap_content"
                 android:orientation="vertical" >
                 <TextView
                     android:layout_width="match_parent"
                     android:layout_height="match_parent"
                     android:text="bb" />
             </LinearLayout>
         </FrameLayout>
     </LinearLayout>
 </TabHost> 

Main.java

 package com.app.main;

 import android.app.TabActivity;
 import android.os.Bundle;
 import android.widget.TabHost;
 import android.widget.TabHost.TabSpec;

 public class Main extends TabActivity {

     @Override
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.main);

         final TabHost tabHost = this.getTabHost();

         TabSpec tab1 = tabHost.newTabSpec("tab1")
                 .setIndicator("tab1")
                 .setContent(R.id.tab1);
         tabHost.addTab(tab1);

         TabSpec tab2 = tabHost.newTabSpec("tab2")
                 .setIndicator("tab2")
                 .setContent(R.id.tab2);
         tabHost.addTab(tab2);
     }
 }

實現效果:

三、註冊切換監聽

若想在使用者點擊 TabWidget 時執行額外邏輯,有兩種常見做法。

做法一,監聽整個 TabHost,回調參數中的 id 就是 TabSpec 的 tag(此例為 "tab1"、"tab2"):

 tabHost.setOnTabChangedListener(new TabChangeListener() {
     public void onTabChanged(String id) {
     }
 });

做法二,直接拿到 TabWidget 的某個子 View 綁定點擊:

 tabWidget.getChildAt(0).setOnClickListener(new OnClickListener() {
     @Override
     public void onClick(View v) {
     }
 });

兩者的差別在於粒度:setOnTabChangedListener 關心的是「切換到了哪個 Tab」,而 getChildAt(i).setOnClickListener 能針對單個標籤按鈕做更細緻的交互處理。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved