編輯:關於 Android 程式設計
本文分享如何以 Android 內建的 TabHost(參考 API Demos 中的 Tabs3)實作一個仿優酷影片客戶端的分頁效果,供需要的朋友參考。
前陣子老師要求自己寫一個影片播放器的客戶端,這是他上課時講到的一個小 demo。透過參考 Android SDK 裡 Tabs3 的寫法,就能做出仿優酷那樣的 TabHost 分頁切換效果。我的 API 路徑是 D:\android\sdk\samples\android-17\ApiDemos\src\com\example\android\apis\view 下的 Tabs3。先看實作效果:

廢話不多說,直接上程式碼:
MainActivity.java
package com.example.video; import android.os.Bundle; import android.R.layout; import android.app.Activity; import android.app.TabActivity; import android.content.Intent; import android.view.LayoutInflater; import android.view.Menu; import android.widget.TabHost; public class MainActivity extends TabActivity { public TabHost tabHost; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //獲取對象 tabHost = getTabHost(); tabHost.addTab(tabHost.newTabSpec("myself") .setIndicator("個人中心") .setContent(new Intent(this, MySelfActivity.class))); tabHost.addTab(tabHost.newTabSpec("myindex") .setIndicator("優酷首頁") .setContent(new Intent(this, MyIndexActivity.class))); tabHost.addTab(tabHost.newTabSpec("mycenter") .setIndicator("頻道中心") .setContent(new Intent(this, MyCenterActivity.class))); //指定的當前的tab //通過索引指定 索引從0開始 tabHost.setCurrentTab(0); //從零開始 //通過標識來激活 // tabHost.setCurrentTabByTag("XXX1"); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } }
MyCenterActivity.java
package com.example.video; import android.app.Activity; import android.os.Bundle; public class MyCenterActivity extends Activity{ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_mycenter); } }
MyIndexActivity.java
package com.example.video; import android.app.Activity; import android.os.Bundle; public class MyIndexActivity extends Activity{ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_myindex); } }
MySelfActivity.java
package com.example.video; import android.app.Activity; import android.os.Bundle; public class MySelfActivity extends Activity{ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_myself); } }
下面是布局檔案:
activity_mycenter.xml
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android" xmlns:tools="https://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="優酷中心" /> </RelativeLayout>
activity_myindex.xml
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="優酷首頁" />
activity_myself.xml
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="個人首頁" />
當然,也別忘了在清單檔中註冊這些 Activity:
<!-- 配置activity組件 --> <activity android:name="com.example.video.MyIndexActivity"/> <activity android:name="com.example.video.MySelfActivity"/> <activity android:name="com.example.video.MyCenterActivity"/>
整體思路很直觀:MainActivity 繼承 TabActivity,透過 getTabHost() 取得 TabHost 物件,再用 newTabSpec 逐一加入各個分頁,每個分頁透過 setIndicator 設定標題、setContent 綁定對應的 Activity;最後用 setCurrentTab 指定啟動時顯示的分頁。這樣就能快速搭出一個多頁切換的客戶端骨架了。
希望本文所述能對你的 Android 程式設計有所幫助。
JVM 記憶體模型入門:程式計數器、虛擬機棧、堆與方法區
JVM 是一個執行在記憶體中的虛擬機。Java 類別、常量、變數與方法執行時,都會落在不同的記憶體區域。理解這些區域,是分析效能、排查 OOM 與理解垃圾回收的基礎。
自定義控件_day01——onLayout 與子 View 的位置計算
自定義 View 其實就三個步驟: 1. 測量——onMeasure():決定 View 的大小2. 布局——onLayout():決定 View 在 ViewGroup
AndroidAnnotations 框架介紹:用註解取代樣板程式碼
開發 Android 應用久了,常會有兩個疑問:為什麼總是不斷重複寫一樣的程式碼?為什麼專案越長大越難維護?context 與 activity 這類「類神」物件、繁瑣的
Android SQLite 資料庫完整實例教學
Android 作為目前主流的行動作業系統,與 SQLite 佔用資源少的優勢十分契合,因此平台內建了一個嵌入式關聯式資料庫——SQLite。若需要在 Android 上