編輯:Android 開發實例
這次要談的是 Tab 元件。Tab 能達到分頁的效果,讓同一個螢幕容納更多內容,是資訊量大的頁面很好的解法;相對地,結構與事件處理也會比單純的 Layout 複雜一些,因此建議在真的有必要時才引入。
Android 的 Tab 元件用起來有點古怪,標籤的巢狀關係與 id 名稱都不能自行更動,必須嚴格按照下面的順序組織:
TabHost → TabWidget(id 必須命名為 tabs)→ FrameLayout(id 必須命名為 tabcontent)。
其中 TabWidget 使用 @android:id/tabs,FrameLayout 使用 @android:id/tabcontent,這兩個名稱是系統查找元件時依循的固定鍵值,寫錯就會在執行期出問題。另外 FrameLayout 要預留上方空間(本例用 paddingTop 讓出 65px),否則內容會被標籤列遮住。
main.xml 的源碼:
- <?xml version="1.0" encoding="utf-8"?>
- <TabHost android:layout_width="fill_parent"
- android:layout_height="fill_parent" xmlns:android="https://schemas.android.com/apk/res/android" android:id="@android:id/TabHost1">
- <TabWidget android:id="@android:id/tabs"
- android:layout_height="wrap_content" android:layout_width="fill_parent">
- </TabWidget>
- <FrameLayout android:id="@android:id/tabcontent"
- android:paddingTop="65px" android:layout_width="fill_parent" android:layout_height="fill_parent">
- <LinearLayout android:layout_height="wrap_content" android:id="@+id/Tab1" android:orientation="vertical" android:layout_width="fill_parent">
- <EditText android:layout_height="wrap_content" android:id="@+id/edtTab1" android:layout_width="fill_parent"></EditText>
- <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/btnTab1" android:text="Tab1"></Button>
- </LinearLayout>
- <LinearLayout android:layout_height="wrap_content" android:id="@+id/Tab2" android:layout_width="fill_parent" android:orientation="horizontal">
- <EditText android:layout_height="wrap_content" android:id="@+id/edtTab2" android:layout_width="wrap_content" android:layout_weight="300"></EditText>
- <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/btnTab2" android:text="Tab2"></Button></LinearLayout>
- </FrameLayout>
- </TabHost>
使用 Tab 的 Activity 必須繼承 TabActivity,再透過 getTabHost() 取得 TabHost 實例。新增分頁的步驟固定為三步:用 newTabSpec 建立規格、setIndicator 設定標籤文字、setContent 關聯要顯示的元件 id,最後 addTab 掛上去。呼叫 setCurrentTab(0) 可以決定啟動時預設停在哪一頁。
值得注意的是,每個 Tab 的內容元件都平放在 FrameLayout 底下,切換時系統只是切換可見性。因此各頁的按鈕、輸入框都能在 onCreate 中一次抓齊並掛上事件,不必等到該頁被選中才處理。
程式源碼:
- package com.testTab;
- import android.app.TabActivity;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.TabHost;
- import android.widget.TabHost.TabSpec;
- public class testTab extends TabActivity {//基於TabActivity構建
- Button btnTab1,btnTab2;
- EditText edtTab1,edtTab2;
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- TabHost tabs = getTabHost();
- //設置Tab1
- TabSpec tab1 = tabs.newTabSpec("tab1");
- tab1.setIndicator("tab1"); // 設置tab1的名稱
- tab1.setContent(R.id.Tab1); // 關聯控件
- tabs.addTab(tab1); // 添加tab1
- btnTab1=(Button)this.findViewById(R.id.btnTab1);
- edtTab1=(EditText)this.findViewById(R.id.edtTab1);
- btnTab1.setOnClickListener(new ClickEvent());
- //設置Tab2
- TabSpec tab2 = tabs.newTabSpec("tab2");
- tab2.setIndicator("tab2");
- tab2.setContent(R.id.Tab2);
- tabs.addTab(tab2);
- btnTab2=(Button)this.findViewById(R.id.btnTab2);
- edtTab2=(EditText)this.findViewById(R.id.edtTab2);
- btnTab2.setOnClickListener(new ClickEvent());
- tabs.setCurrentTab(0);
- }
- class ClickEvent implements View.OnClickListener {
- @Override
- public void onClick(View v) {
- if(v==btnTab1)
- {
- edtTab1.setText("tab1");
- }
- else if(v==btnTab2)
- {
- edtTab2.setText("tab2");
- }
- }
- }
- }
Android EditText 實戰:提示文字、焦點切換與輸入長度限制
EditText 是使用者輸入資料最主要的入口。本文示範如何做出「未輸入時顯示灰色提示、開始輸入後提示消失」的常見效果,並在超過上限時給出明確提醒。 學習目標 1. 學
Android 仿微信朋友圈九宮格圖片查看器實現
打開微信朋友圈,你會發現一個經典的「九宮格」圖片區域,點擊任意一張圖就能進入大圖查看頁,還支援滑動切換與縮放。這樣的交互幾乎是所有社交類應用的標配。本文就帶你從零實現一個
Android Service 全面整理:分類、生命週期與使用方式
Service 是 Android 四大元件之一,但也最常被誤解成「執行緒」。這篇把它的分類、與 Thread 的差異、生命週期、啟動與綁定方式,以及 manifest
以百度地圖 SDK 實現 Android 定位功能:完整流程解析
LBS 位置服務是 Android 應用中愈來愈常見的能力,從定位、地圖顯示到導航,都建立在它之上。本文以百度地圖定位 SDK 為例,逐步示範如何完成一次定位並把結果回傳