android 切換卡(TabWidget)

abWidget類似於Android 中查看電話薄的界面,通過多個標籤切換顯示不同內容。要實現這一效果,首先要了解TabHost,它是一個用來存放多個Tab標籤的容器。每一個Tab都可以對應自己的佈局,比如,電話薄中的Tab佈局就是一個List的線性佈局了。 
   要使用TabHost,首先需要通過getTabHost方法來獲取TabHost的對象,然後通過addTab方法來向TabHost中添加 Tab。當然每個Tab在切換時都會產生一個事件,要捕捉這個事件需要設置TabActivity的事件監聽 setOnTabChangedListener。我們先來看看運行效果吧。 


 
 
 


佈局文件 
<?xml version="1.0" encoding="utf-8"?> 
<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"/> 
    <FrameLayout 
    android:id="@android:id/tabcontent" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 
    <TextView 
    android:id="@+id/textview1" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:text="Linux" 
    android:textColor="#FF0000" 
    /> 
    <TextView 
    android:id="@+id/textview2" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:textColor="#385E0F" 
    android:text="MAC"/> 
    <TextView 
    android:id="@+id/textview3" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:textColor="#1E90FF" 
    android:text="Window"/>
</FrameLayout> 
    </LinearLayout> 
</TabHost> 
這裏稍有不同,用的是 TabHost   然後 LinearLayout裏邊套了一個 FrameLayout 
LinearLayout 就不說了 這裏說一下 FrameLayout 的特點 FrameLayout是最簡單的一個佈局對象。它被定製爲你屏幕上的一個空白備用區域,之後你可以在其中填充一個單一對象 — 比如,一張你要發佈的圖片。所有的子元素將會固定在屏幕的左上角;你不能爲FrameLayout中的一個子元素指定一個位置。後一個子元素將會直接在前一個子元素之上進行覆蓋填充,把它們部份或全部擋住(除非後一個子元素是透明的)。這裏最重要的特點就是 後一個子元素將會直接在前一個子元素之上進行覆蓋填充,把它們部分或全部擋住 我們也正是利用了它的這一特點。 大家看看運行效果就知道咋回事了。這裏有個關於佈局對象的簡單講解http://blog.csdn.net/Android_Tutor/archive/2009/11/06/4779097.aspx 

下邊是 Activity 類 
Java代碼  收藏代碼
  1. package xiaohang.zhimeng;  
  2.   
  3. import android.app.AlertDialog;  
  4. import android.app.Dialog;  
  5. import android.app.TabActivity;  
  6. import android.content.DialogInterface;  
  7. import android.graphics.Color;  
  8. import android.os.Bundle;  
  9. import android.widget.TabHost;   
  10. import android.widget.TabHost.OnTabChangeListener;  
  11.   
  12. //這裏注意一下,繼承的是TabActivity 不是Activity  
  13. public class Activity01 extends TabActivity {  
  14.   
  15.     // 聲明TabHost對象  
  16.     TabHost xh_TabHost;  
  17.   
  18.     @Override  
  19.     public void onCreate(Bundle savedInstanceState) {  
  20.         super.onCreate(savedInstanceState);  
  21.         setContentView(R.layout.main);  
  22.   
  23.         // 取得TabHost對象  
  24.         xh_TabHost = getTabHost();  
  25.   
  26.         /** 
  27.          * 爲TabHost添加標籤 新建一個newTabSped(newTabSpec) 設置其標籤和圖標(setIndicator) 
  28.          * 設置內容(setContent) 
  29.          */  
  30.         // TabSpec 是TabHost的內部類 TabHost對象的 newTabSpec()方法返回一個TabSpec對象 這個關係要搞清楚  
  31.         /* 
  32.          * 源碼裏邊是這麼寫的 public TabSpec newTabSpec(String tag) { return new 
  33.          * TabSpec(tag); } 
  34.          */  
  35.   
  36.         xh_TabHost.addTab(xh_TabHost.newTabSpec("tab_test1")  
  37.         // setIndicator()此方法用來設置標籤和圖表  
  38.                 .setIndicator("TAB 1",  
  39.                         getResources().getDrawable(R.drawable.img1))  
  40.                 // 指定內容爲一個TextView --->public TabHost.TabSpec setContent (int  
  41.                 // viewId) 此方法需要一個 viewId 作爲參數  
  42.                 .setContent(R.id.textview1));  
  43.   
  44.         xh_TabHost.addTab(xh_TabHost.newTabSpec("tab_test2").setIndicator(  
  45.                 "TAB 2", getResources().getDrawable(R.drawable.img2))  
  46.                 .setContent(R.id.textview2));  
  47.   
  48.         xh_TabHost.addTab(xh_TabHost.newTabSpec("tab_test3").setIndicator(  
  49.                 "TAB 3", getResources().getDrawable(R.drawable.img3))  
  50.                 .setContent(R.id.textview3));  
  51.   
  52.         // 設置TabHost的背景顏色  
  53.         xh_TabHost.setBackgroundColor(Color.argb(1502270150));  
  54.   
  55.         // 設置TabHost的背景圖片資源  
  56.         xh_TabHost.setBackgroundResource(R.drawable.bg2);  
  57.   
  58.         // 設置當前顯示哪一個標籤 我的理解就是當你第一次啓動程序默認顯示那個標籤 這裏是指定的選項卡的ID從0開始  
  59.         xh_TabHost.setCurrentTab(0);  
  60.   
  61.         // 標籤切換事件處理,setOnTabChangedListener 注意是標籤切換事件不是點擊事件  
  62.         // 就是從一個標籤切換到另外一個標籤會觸發的事件  
  63.         xh_TabHost.setOnTabChangedListener(new OnTabChangeListener() {  
  64.             @Override  
  65.             public void onTabChanged(String tabId) {  
  66.                 // 定義一個彈出式的對話框  
  67.                 Dialog dialog = new AlertDialog.Builder(Activity01.this)  
  68.                         .setTitle("提示").setMessage("當前選中了:" + tabId + "標籤")  
  69.                         .setPositiveButton("確定",  
  70.                                 new DialogInterface.OnClickListener() {  
  71.                                     @Override  
  72.                                     public void onClick(DialogInterface dialog,  
  73.                                             int which) {  
  74.                                         // 取消對話框  
  75.                                         dialog.cancel();  
  76.                                     }  
  77.   
  78.                                 }).create();// 創建出一個“確定”按鈕  
  79.                 // 啓動此對話框並且顯示在屏幕上  
  80.                 dialog.show();  
  81.             }  
  82.         });  
  83.     }  
  84. }  

  85. 測試代碼:點擊打開鏈接
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章