編輯:關於 Android 程式設計
最近仔細研究了 TabHost,目標是做一個微信那樣的底部導航欄。TabHost 的用法大致可分為兩類:不繼承 TabActivity,以及繼承 TabActivity。顯然後者寫起來更省事,下面把三種典型寫法逐一走一遍。
activity_main.xml:注意 TabHost 內部的 id 必須使用系統預定義值,@android:id/tabs 與 @android:id/tabcontent 都不能改。
<LinearLayout 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:orientation="vertical"
tools:context=".MainActivity" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
<TabHost
android:id="@+id/tabhost"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TabWidget
android:id="@android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</TabWidget>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<!-- 第一個tab的佈局 -->
<LinearLayout
android:id="@+id/tab1"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="林炳東" />
</LinearLayout>
<!-- 第二個tab的佈局 -->
<LinearLayout
android:id="@+id/tab2"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="張小媛" />
</LinearLayout>
<!-- 第三個tab的佈局 -->
<LinearLayout
android:id="@+id/tab3"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="馬貝貝" />
</LinearLayout>
</FrameLayout>
</LinearLayout>
</TabHost>
</LinearLayout>
代碼:手動 findViewById 拿到 TabHost,呼叫 setup() 初始化,再用 newTabSpec 逐個添加。
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TabHost th = (TabHost) findViewById(R.id.tabhost);
th.setup(); //初始化TabHost容器
//在TabHost創建標簽,然後設置:標題/圖標/標簽頁佈局
th.addTab(th.newTabSpec("tab1")
.setIndicator("標簽1", getResources().getDrawable(R.drawable.ic_launcher))
.setContent(R.id.tab1));
th.addTab(th.newTabSpec("tab2")
.setIndicator("標簽2", null)
.setContent(R.id.tab2));
th.addTab(th.newTabSpec("tab3")
.setIndicator("標簽3", null)
.setContent(R.id.tab3));
//上面傳 null 的位置,可以改為 getResources().getDrawable(R.drawable.圖片名) 來設置圖標
}
}

這種寫法的好處是標籤內容與主佈局解耦,佈局檔不會越寫越腫。
tab1.xml
<?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="我是標簽1的內容喔"
android:id="@+id/TextView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
</LinearLayout>
tab2.xml
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:id="@+id/LinearLayout02"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:text="標簽2"
android:id="@+id/TextView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
activity_main.xml:這次 FrameLayout 裡是空的,內容留給代碼動態填充。
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TabHost
android:id="@+id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TabWidget
android:id="@android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</TabWidget>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</FrameLayout>
</LinearLayout>
</TabHost>
</LinearLayout>
代碼:用 LayoutInflater 把各個 XML 加載到 getTabContentView() 中,setContent 傳入的是佈局根節點的 id。
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TabHost m = (TabHost) findViewById(R.id.tabhost);
m.setup();
LayoutInflater i = LayoutInflater.from(this);
i.inflate(R.layout.tab1, m.getTabContentView());
i.inflate(R.layout.tab2, m.getTabContentView()); //動態載入XML,不需要Activity
m.addTab(m.newTabSpec("tab1").setIndicator("標簽1").setContent(R.id.LinearLayout01));
m.addTab(m.newTabSpec("tab2").setIndicator("標簽2").setContent(R.id.LinearLayout02));
}
}

activity_main.xml:這裡只放各頁內容,TabWidget 由 TabActivity 的默認佈局提供。
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<!-- 第一個佈局 -->
<LinearLayout
android:id="@+id/view1"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="張小媛" />
</LinearLayout>
<!-- 第二個佈局 -->
<LinearLayout
android:id="@+id/view2"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="馬貝貝" />
</LinearLayout>
<!-- 第三個佈局 -->
<TextView
android:id="@+id/view3"
android:background="#00ff00"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="Tab3"/>
</FrameLayout>
代碼:把父類換成 TabActivity,用 getTabHost() 直接拿到實例,還可以透過 setOnTabChangedListener 處理切換事件。
public class MainActivity extends TabActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTitle("TabDemoActivity");
TabHost tabHost = getTabHost();
LayoutInflater.from(this).inflate(R.layout.activity_main,
tabHost.getTabContentView(), true);
tabHost.addTab(tabHost.newTabSpec("tab1")
.setIndicator("tab1", getResources().getDrawable(R.drawable.ic_launcher))
.setContent(R.id.view1));
tabHost.addTab(tabHost.newTabSpec("tab2")
.setIndicator("tab2")
.setContent(R.id.view2));
tabHost.addTab(tabHost.newTabSpec("tab3")
.setIndicator("tab3")
.setContent(R.id.view3));
//標簽切換事件處理
tabHost.setOnTabChangedListener(new OnTabChangeListener() {
@Override
public void onTabChanged(String tabId) {
if (tabId.equals("tab1")) {
//第一個標簽
}
if (tabId.equals("tab2")) {
//第二個標簽
}
if (tabId.equals("tab3")) {
//第三個標簽
}
}
});
}
}

把 TabWidget 隱藏、換上一組 RadioButton,就能做出更自由的底部菜單樣式,同時還能顯示未讀消息數量。

延伸閱讀:Android仿微信底部菜單欄功能顯示未讀消息數量。
原文地址:https://blog.csdn.net/harvic880925/article/details/17120325
三種寫法各有取捨:寫法一最直觀但佈局容易膨脹;寫法二解耦最乾淨;寫法三代碼量最少,但 TabActivity 本身已被標記為老舊 API,新專案建議改用 Fragment + TabLayout。
Android 資料儲存入門:SQLiteDatabase 與 SQLiteOpenHelper 實務整理
SQLite 是 Android 常用的本機資料庫。本文整理 SQLiteDatabase 的開啟、建表、新增、修改、刪除、查詢,以及 SQLiteOpenHelper
Android 使用 SharedPreferences 實現數據存儲與讀取
開發中我們經常需要為用戶提供參數設置功能,比如 QQ 裡是否允許陌生人加我為好友這類開關。保存這類配置參數,桌面軟件通常用 ini 文件,J2SE 應用則多用 prope
Android RelativeLayout 相對佈局完全指南
相對佈局的概念 RelativeLayout 透過元件之間的相對關係排列畫面,而不是像 LinearLayout 只依方向堆疊。它能減少多層嵌套,讓版面更靈活,也能提升部
Android AnimationListener 監聽動畫狀態的用法
AnimationListener 的作用 AnimationListener 用來監聽動畫的生命週期,包括開始、結束與重複。透過它,可以在動畫完成後執行額外處理,例如移