Android TabHost TabWidget切換卡的實現應用

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


1、佈局文件

<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:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <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:text="MAC"
        android:textColor="#385E0F" />

      <TextView
        android:id="@+id/textview3"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:text="Windows"
        android:textColor="#1E90FF" />
    </FrameLayout>
  </LinearLayout>

</TabHost>

2、修改MainActivity,注意是繼承自TabActivity

public class MainActivity extends TabActivity {

 private TabHost tabHost;

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

 tabHost = getTabHost();

 addTab();// 添加標籤
 // 設置TabHost背景顏色
 tabHost.setBackgroundColor(Color.argb(150, 20, 80, 150));
 // 設置TabHost背景圖片資源
 tabHost.setBackgroundResource(R.drawable.ic_launcher);
 // 設置當前顯示哪一個標籤 我的理解就是當你第一次啓動程序默認顯示那個標籤 這裏是指定的選項卡的ID從0開始
 tabHost.setCurrentTab(0);
 // 標籤切換事件處理,setOnTabChangedListener 注意是標籤切換事件不是點擊事件,而是從一個標籤切換到另外一個標籤會觸發的事件
 tabHost.setOnTabChangedListener(new OnTabChangeListener() {
  @Override
  public void onTabChanged(String tabId) {
  AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
  Dialog dia;
  builder.setTitle("提示");
  builder.setMessage("當前選中了" + tabId + "標籤");
  builder.setPositiveButton("確定", new OnClickListener() {
   @Override
   public void onClick(DialogInterface dialog, int which) {
   dialog.cancel();
   }
  });
  dia = builder.create();
  dia.show();
  }
 });
 }

 // 爲TabHost添加標籤 新建一個newTabSped(new TabSpec) 設置其標籤和圖標(setIndicator)、設置內容(setContent)
 // TabSpec是TabHost的內部類 TabHost對象的 newTabSpec()方法返回一個TabSpec對象
 // 源碼裏邊是這麼寫的 public TabSpec newTabSpec(String tag)
 // { return new TabSpec(tag); }
 private void addTab() {
 tabHost.addTab(tabHost
  .newTabSpec("tab1")
  .setIndicator("TAB1",
   getResources().getDrawable(R.drawable.ic_launcher))// setIndicator()此方法用來設置標籤和圖表
  .setContent(R.id.textview1));
 // 指定內容爲一個TextView --->public TabHost.TabSpec setContent(int viewId) 此方法需要一個 viewId 作爲參數
 tabHost.addTab(tabHost
  .newTabSpec("tab2")
  .setIndicator("TAB2",
   getResources().getDrawable(R.drawable.ic_launcher))
  .setContent(R.id.textview2));

 tabHost.addTab(tabHost
  .newTabSpec("tab3")
  .setIndicator("TAB3",
   getResources().getDrawable(R.drawable.ic_launcher))
  .setContent(R.id.textview3));
 }
}

 3、運行程序:如下!




轉自:http://www.jb51.net/article/35747.htm



發佈了74 篇原創文章 · 獲贊 100 · 訪問量 26萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章