Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

TabHost三種實現方式詳解:不繼承、動態加載與TabActivity

編輯:關於 Android 程式設計

最近仔細研究了 TabHost,目標是做一個微信那樣的底部導航欄。TabHost 的用法大致可分為兩類:不繼承 TabActivity,以及繼承 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.圖片名) 來設置圖標
     }
 }

方法二:把每個 Tab 的內容拆成獨立 XML,動態 inflate

這種寫法的好處是標籤內容與主佈局解耦,佈局檔不會越寫越腫。

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));
     }
 }

方法三:繼承 TabActivity

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。

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