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

Android UI 元件系列:Tab Layout 選項卡版面配置

編輯:Android 資訊

要建立一個選項卡的 UI,你需要使用 TabHost 與 TabWidget。TabHost 必須是版面配置檔的根節點,它包含了用來顯示選項卡的 TabWidget,以及一個用於顯示選項內容的 FrameLayout。

你可以用兩種方式之一來實作選項卡內容:一是在單一 Activity 中透過選項卡在各檢視之間切換,二是用選項卡來切換各個獨立的 Activity。可根據需求選擇適合的方式;但如果每個選項卡都提供獨立的使用者體驗,那麼為每個選項卡實作獨立的 Activity 會更有意義。把應用邏輯分散管理,通常會比把大量程式碼與版面配置塞在一起更好維護。

在這個範例中,我們將為每個獨立的 Activity 建立對應的選項卡。

1、開始一個新工程,命名為 HelloTabWidget。

2、首先,在你的工程中建立三個獨立的 Activity:ArtistsActivity、AlbumsActivity 與 SongsActivity,它們各自代表一個選項卡。現在先用 TextView 為每個程式顯示一段簡單的訊息,例如:

package org.hualang.tabwidget; import android.app.Activity; import android.os.Bundle; import android.widget.TextView; public class AlbumsActivity extends Activity { public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); TextView textview = new TextView(this); textview.setText("This is the Albums tab"); setContentView(textview); } }
package org.hualang.tabwidget; import android.app.Activity; import android.os.Bundle; import android.widget.TextView; public class SongsActivity extends Activity { public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); TextView textview = new TextView(this); textview.setText("This is the Songs tab"); setContentView(textview); } }
package org.hualang.tabwidget; import android.app.Activity; import android.os.Bundle; import android.widget.TextView; public class ArtistsActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); TextView textview = new TextView(this); textview.setText("This is the Artists tab"); setContentView(textview); } }

請注意,這個範例不需要版面配置檔,只需建立一個 TextView 並為文字賦值即可。重複建立三個類似的 Activity,並務必在 AndroidManifest.xml 中註冊,否則會報錯。

3、你需要為每個選項卡設定一個 icon。每個 icon 通常有兩個版本,一個是選項卡被選取時顯示,另一個是未被選取時顯示。一般設計上,建議選取時用灰色,未選取時用白色,例如:


你可以複製這兩張圖片來做實驗。

接著建立一個狀態圖片清單,用來指定每個選項卡在不同狀態下所對應的圖片:

①把圖片儲存在 res/drawable/ 目錄下
②在 res/drawable/ 目錄下建立一個名為 ic_tab_artists.xml 的檔案,並插入以下內容。

<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="https://schemas.android.com/apk/res/android"> <!-- When selected,use grey --> <item android:drawable="@drawable/ic_tab_artists_grey" android:state_selected="true"/> <!-- When not selected ,use white --> <item android:drawable="@drawable/ic_tab_artists_white"/> </selector>

有了這個檔案,當選項卡的狀態改變時,它就會自動在兩種已定義的圖片之間切換。

4、打開 res/layout/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" android:padding="5dp"> <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" android:padding="5dp" /> </LinearLayout> </TabHost>

這個版面配置檔將顯示選項卡,並提供各 Activity 之間的導航。TabHost 要求一個 TabWidget 與一個 FrameLayout,為了讓兩者處於垂直方向,需要一個 LinearLayout;FrameLayout 則是每個選項卡內容的顯示位置。之所以那裡的內容是空的,是因為 TabHost 會自動為每個 Activity 嵌入內容。

請注意,TabWidget 與 FrameLayout 元素的 ID(tabcontent 等)必須使用這些名稱,因為 TabHost 會依據這些名稱來檢索它們的引用。

5、編寫 HelloTabWidget,繼承 TabActivity。

package org.hualang.tabwidget; import android.app.TabActivity; import android.content.Intent; import android.content.res.Resources; import android.os.Bundle; import android.widget.TabHost; public class HelloTabWidget extends TabActivity { public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Resources res = getResources(); // Resource object to get Drawables TabHost tabHost = getTabHost(); // The activity TabHost TabHost.TabSpec spec; // Resusable TabSpec for each tab Intent intent; // Reusable Intent for each tab // Create an Intent to launch an Activity for the tab (to be reused) intent = new Intent().setClass(this, ArtistsActivity.class); // Initialize a TabSpec for each tab and add it to the TabHost spec = tabHost.newTabSpec("artists").setIndicator("Artists", res.getDrawable(R.drawable.ic_tab_drawable)) .setContent(intent); tabHost.addTab(spec); // Do the same for the other tabs intent = new Intent().setClass(this, AlbumsActivity.class); spec = tabHost.newTabSpec("albums").setIndicator("Albums", res.getDrawable(R.drawable.ic_tab_drawable)) .setContent(intent); tabHost.addTab(spec); intent = new Intent().setClass(this, SongsActivity.class); spec = tabHost.newTabSpec("songs").setIndicator("Songs", res.getDrawable(R.drawable.ic_tab_drawable)) .setContent(intent); tabHost.addTab(spec); tabHost.setCurrentTab(2); } }

執行結果:

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