Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
您现在的位置: Android教程網 >> Android系統教程 >  >> Android 技術 >> Android 資訊

Android UI 元件系列:TabWidget 切換卡

編輯:Android 資訊

Tab 選項卡的樣式類似於電話簿的介面,透過多個標籤切換不同的內容。要實現這個效果,首先必須了解 TabHost——它是一個用來容納多個 Tab 標籤的容器,每個 Tab 都能對應自己的版面配置,例如電話簿中的 Tab 版面就是一個線性版面。

使用 TabHost 時,首先要透過 getTabHost 方法取得 TabHost 物件,再以 addTab 方法向其中加入 Tab。每個 Tab 在切換時都會產生一個事件,若要捕捉這個事件,需設定 TabActivity 的事件監聽 setOnTabChangedListener。

以下是一個小範例:

TabTest.java

package org.hualang.tab; import android.app.Activity; import android.app.TabActivity; import android.graphics.Color; import android.os.Bundle; import android.widget.TabHost; import android.widget.Toast; import android.widget.TabHost.OnTabChangeListener; public class TabTest extends TabActivity { /** Called when the activity is first created. */ TabHost tabhost; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //取得TabHost對象 tabhost = getTabHost(); //為TabHost添加標簽 //新建一個newTabSpec(newTabSpec) //設置其標簽和圖標(setIndicator) //設置內容(setContent) tabhost.addTab(tabhost.newTabSpec("tab1") .setIndicator("TAB 1",getResources().getDrawable(R.drawable.img1)) .setContent(R.id.text1)); tabhost.addTab(tabhost.newTabSpec("tab2") .setIndicator("TAB 2",getResources().getDrawable(R.drawable.img2)) .setContent(R.id.text2)); tabhost.addTab(tabhost.newTabSpec("tab3") .setIndicator("TAB 3",getResources().getDrawable(R.drawable.img3)) .setContent(R.id.text3)); //設置TabHost的背景顏色 //tabhost.setBackgroundColor(Color.argb(150,22,70,150)); //設置TabHost的背景圖片資源 tabhost.setBackgroundResource(R.drawable.bg0); //設置當前顯示哪個標簽 tabhost.setCurrentTab(0); //標簽切換事件處理,setOnTabChangedListener tabhost.setOnTabChangedListener(new OnTabChangeListener() { public void onTabChanged(String tabId) { Toast toast=Toast.makeText(getApplicationContext(), "現在是"+tabId+"標簽", Toast.LENGTH_SHORT); toast.show(); } }); } }

main.xml

<?xml version="1.0" encoding="utf-8"?> <TabHost xmlns:android="https://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/text1" android:layout_width="fill_parent" android:layout_height="fill_parent" android:text="選項卡1" /> <TextView android:id="@+id/text2" android:layout_width="fill_parent" android:layout_height="fill_parent" android:text="選項卡2" /> <TextView android:id="@+id/text3" android:layout_width="fill_parent" android:layout_height="fill_parent" android:text="選項卡3" /> </FrameLayout> </LinearLayout> </TabHost>

執行效果如下:

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