編輯:Android 開發實例
本文整理一個可直接運行的 Android 選項卡範例,架構參考官方 SDK 文件中的 hello-tabwidget 教學,並為求簡潔省略了部分自製圖示,直接沿用系統內建資源。
這裡先建立 OneActivity 與 TestActivity,各自用程式碼動態產生一個 TextView 當作畫面內容,省去額外配置檔的麻煩。
public class OneActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView textview = new TextView(this);
textview.setText("This is the Artists tab");
setContentView(textview);
}
}
這個檔案決定了分頁長什麼樣子。有幾個地方一定要盯緊:
@android:id/tabhost、@android:id/tabs、@android:id/tabcontent,一個字都不能改。android:orientation="vertical",因為 LinearLayout 預設是水平排列。如果執行後畫面和預期差很多,回頭檢查這幾點通常就能找到原因。
<?xml version="1.0" encoding="utf-8"?>
<TabHost android:id="@android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="https://schemas.android.com/apk/res/android">
<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">
</TabWidget>
<FrameLayout android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="5dp" />
</LinearLayout>
</TabHost>
這個類別負責把版面與內容綁在一起。每個分頁透過 newTabSpec() 建立,再用 setIndicator() 設定標題與圖示,最後以 setContent() 指定內容來源——可以是 Intent,也可以是版面中的資源 ID。
package com.fatkun;
import android.app.TabActivity;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
public class TabWidget extends TabActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tab); // 使用上一個步驟建立的版面
Resources res = getResources(); // 用來取得 Drawable 資源
TabHost tabHost = getTabHost(); // 取得 TabHost 容器
TabSpec spec;
Intent intent; // 每個分頁重複使用的 Intent
// 第一個分頁
intent = new Intent(this, OneActivity.class);
spec = tabHost.newTabSpec("tab1")
.setIndicator("Tab1", res.getDrawable(android.R.drawable.ic_media_play))
.setContent(intent); // 這裡也可以填 R.id.xxx
tabHost.addTab(spec);
// 第二個分頁
intent = new Intent(this, TestActivity.class);
spec = tabHost.newTabSpec("tab2")
.setIndicator("Tab2", res.getDrawable(android.R.drawable.ic_menu_camera))
.setContent(intent);
tabHost.addTab(spec);
tabHost.setCurrentTab(1); // 預設停在第二個分頁
}
}
主容器加上 android:theme="@android:style/Theme.NoTitleBar",可以隱藏標題列,把寶貴的垂直空間留給分頁內容;被當成分頁的 Activity 也必須逐一註冊,否則執行時會找不到元件。
<activity android:name="TabWidget"
android:theme="@android:style/Theme.NoTitleBar"></activity>
<activity android:name="OneActivity"></activity>
<activity android:name="TestActivity"></activity>
這個範例最值得記住的觀念是:TabWidget 只是「頁籤外觀」,真正負責管理與切換的是 TabHost,而分頁內容的來源有兩種寫法——Intent 導向另一個 Activity,或直接指向同一版面中的某個 View。前者結構鬆散、易於擴充,是實務上較常採用的做法。
Android MediaPlayer 完整指南:播放控制、進度條與快轉倒轉
要在 App 裡播放音訊或影片,Android 提供了不少途徑,而 MediaPlayer 是最直覺的一個。它讓你以幾行程式碼就能控制內建媒體播放服務,完成播放、暫停、跳
Android MediaPlayer 音訊播放:播放、暫停與快轉
MediaPlayer 是 Android 中常用的多媒體播放類別。除了基本的播放與暫停,也可以透過 seekTo() 實作快轉與快退。 初始化播放器 MediaPlay
動手做一個 Android 塗鴉板:SurfaceView 實作教學(附完整程式碼)
寫 Android 小專案是練功的好方式。它不像做作業那樣帶著壓力,卻能在動手過程中把課本上的知識真正變成自己的東西。這次我們就用 SurfaceView 做一個可以隨手
動手打造Android電子書書架:自定義GridView實現原理全解析
做電子書的開發者,大概都對「書架」這個效果又愛又恨。早期我曾用 ListView 硬湊過一版,效果差強人意;後來才發現,絕大多數優秀的書架其實都是靠 GridView 實