編輯: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>
執行效果如下:

用 PHP 打造 Android 應用程式的完整教學
Google 的 Android 作業系統是開源專案,與對上架審核嚴格把關的 Apple App Store 相比,Android 平台開放得多。這種開放甚至延伸到了語言
Android 渲染優化指南:降低卡頓與過度繪製
使用者對 Android 應用的第一印象,往往來自畫面是否流暢。若動畫不順、列表滑動卡頓,即使功能完整,也會大幅降低體驗。渲染優化的核心,是理解每一幀的時間預算,並減少不
動手封裝 BaseAdapter:讓 ListView 寫出 RecyclerView 的手感
雖然新專案大多已經轉向 RecyclerView,但在不少場景下,ListView 與 GridView 依然是更直接的選擇,而選擇它們也就意味著要寫更多 Adapter
一個簡單易用的 Android 頂部導航欄 TitleBar
這是一個簡單易用的導航欄 TitleBar,能輕鬆實現各種類似 iOS 導航欄的效果。全部代碼集中在 TitleBar.java 中,所有控件都動態生成、動態佈局,無需引