編輯:Android 開發實例
TabHost 讓你可以在同一個螢幕裡切換不同版面,Android 自帶的撥號程式就是最直白的例子:通話紀錄、聯絡人、撥號鍵盤共用一個頁面,靠頁籤切來切去。
翻開 TabHost 的原始碼,主要的成員變數只有三個:
private TabWidget mTabWidget;
private FrameLayout mTabContent;
private List<TabSpec> mTabSpecs;
這也意味著你的 XML 佈局必須提供 TabWidget 與 FrameLayout,因為建構過程會直接檢查,缺了就丟例外:
if (mTabWidget == null) {
throw new RuntimeException(
"Your TabHost must have a TabWidget whose id attribute is 'android.R.id.tabs'");
}
mTabContent = (FrameLayout) findViewById(com.android.internal.R.id.tabcontent);
if (mTabContent == null) {
throw new RuntimeException(
"Your TabHost must have a FrameLayout whose id attribute is 'android.R.id.tabcontent'");
}
至於 mTabSpecs,它就是你註冊的頁籤清單,初始容量給 2,不夠會自動擴充:
private List<TabSpec> mTabSpecs = new ArrayList<TabSpec>(2);
先寫兩個內容佈局。tab1.xml 用 LinearLayout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:id="@+id/LinearLayout01"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView android:text="tab1 with linear layout"
android:id="@+id/TextView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
tab2.xml 則是 FrameLayout 裡再包一層 LinearLayout:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:id="@+id/FrameLayout02"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<LinearLayout android:id="@+id/LinearLayout02"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView android:text="tab2"
android:id="@+id/TextView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</FrameLayout>
繼承 TabActivity,直接呼叫 getTabHost(),它內部做的就是:
if (mTabHost == null) {
this.setContentView(com.android.internal.R.layout.tab_content);
}
容器有了,接著 inflate 內容、註冊 TabSpec:
TabHost mTabHost = getTabHost();
LayoutInflater inflater_tab1 = LayoutInflater.from(this);
inflater_tab1.inflate(R.layout.tab1, mTabHost.getTabContentView());
inflater_tab1.inflate(R.layout.tab2, mTabHost.getTabContentView());
mTabHost.addTab(mTabHost.newTabSpec("tab_test1")
.setIndicator("TAB 11").setContent(R.id.LinearLayout01));
mTabHost.addTab(mTabHost.newTabSpec("tab_test1")
.setIndicator("TAB 11").setContent(R.id.FrameLayout02));
注意這種寫法不需要自己呼叫 setContentView(),因為 getTabHost() 已經代你做完了。
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="https://schemas.android.com/apk/res/android"
android:id="@+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">
</FrameLayout>
</LinearLayout>
</TabHost>
除了 TabHost 自己的 id 可以自訂,其餘必須沿用系統 id——原因就在前面那段原始碼裡。取得元件的方式改成 findViewById,並且一定要補上 setup():
setContentView(R.layout.main);
TabHost mTabHost = (TabHost) findViewById(R.id.tabhost);
mTabHost.setup();
LayoutInflater inflater_tab1 = LayoutInflater.from(this);
inflater_tab1.inflate(R.layout.tab1, mTabHost.getTabContentView());
inflater_tab1.inflate(R.layout.tab2, mTabHost.getTabContentView());
mTabHost.addTab(mTabHost.newTabSpec("tab_test1")
.setIndicator("TAB a").setContent(R.id.LinearLayout01));
mTabHost.addTab(mTabHost.newTabSpec("tab_test2")
.setIndicator("TAB b").setContent(R.id.FrameLayout02));
官方註解把這件事說得很明白:用 findViewById 載入 TabHost 時,加頁籤前必須先呼叫 setup();但透過 TabActivity 的 getTabHost() 取得則不需要。setup 的作用正是把 mTabWidget 與 mTabContent 這兩個成員初始化好,這也正好解釋了為什麼那兩個 id 一定要用系統定義的。
兩條路線的執行結果相同,差別只在於你要不要自己維護那份佈局檔。
使用 OpenGL ES 混合模式縮放視頻緩衝區以適配螢幕
在開發軟體渲染型遊戲時,如何將固定尺寸的視頻緩衝區縮放到不同解析度螢幕,是常見挑戰。相較於純軟體縮放或完全依賴 GPU,混合模式在效能與相容性之間取得較好平衡。三種縮放策
Android 自定義 ImageView 實現透明圓弧與弧形文字
最近因為項目需求,需要在 ImageView 上疊加一層透明圓弧,並且沿著圓弧方向顯示相應的文字,效果如下圖所示: 拿到這個需求,我最直覺的思路就是自定義一個 Imag
自己動手做朋友圈:Android 動態列表、多圖選擇與影片錄製全流程
最近寫了一個簡單的「朋友圈」應用,涵蓋動態列表、影片錄製/預覽/上傳,以及圖片來源可選拍照或相冊;從相冊選取時支援一次多選,並限制最多張數。想擁有真正屬於自己的朋友圈嗎?
Android 組件樣式定製方法詳解(以 RadioButton 為例)
前言Android 中有許多現成的組件可以使用,但實際開發時,系統自帶的組件往往不太合用,主要是樣式可能並非我們想要的。這個時候,我們就需要定製一些樣式。本文將講解如何修