自Android3.2之後,TabActibvity被棄用(Deprecated),取而代之的是FragmentActivity,因為Fragment比Activiy更靈活,消耗的資源更小,完全能夠滿足TabActivity的效果,所以直接替代之。原來的TabActibvity+TabHost+Activity那套還可以用,不過強烈建議改用FragmentActivity+FragmentTabHost+Fragement
FragmentTabHost用法:
1. 定義FragmentActivity的layout:
[html] view plaincopy
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:orientation="vertical" >
-
-
- <FrameLayout
- android:id="@+id/realtabcontent"
- android:layout_width="fill_parent"
- android:layout_height="0dip"
- android:layout_weight="1" />
-
-
- <android.support.v4.app.FragmentTabHost
- android:id="@android:id/tabhost"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:background="@drawable/maintab_toolbar_bg">
-
-
- <FrameLayout
- android:id="@android:id/tabcontent"
- android:layout_width="0dp"
- android:layout_height="0dp"
- android:layout_weight="0" />
- </android.support.v4.app.FragmentTabHost>
-
-
- </LinearLayout>
2. 必須繼承FragmentActivity
[java] view plaincopy
- public class MainTabActivity extends FragmentActivity{
- //定義FragmentTabHost對象
- private FragmentTabHost mTabHost;
3. 得到FragmentTabHost對象
[java] view plaincopy
- mTabHost = (FragmentTabHost)findViewById(android.R.id.tabhost);
4. 初始化FragmentTabHost對象
[java] view plaincopy
- mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);
注意,這裡的R.id.realtabcontent可以是任一個ViewGroup或其子類的對象id,比如LinearLayout。其實際作用就是個容器,Tab切換時,當前Tab對應的Fragment會被加入到這個ViewGroup作為其子View
5.按順序添加每個Tab頁
[java] view plaincopy
- //為每一個Tab按鈕設定表徵圖、文字和內容
- TabSpec tabSpec = mTabHost.newTabSpec("TAG1").setIndicator(yourTabItemView);
- //將Tab按鈕添加進Tab選項卡中
- mTabHost.addTab(tabSpec, fragmentPage1.class, null);
- //設定Tab按鈕的背景
- mTabHost.getTabWidget().getChildAt(i).setBackgroundResource(R.drawable.selector_tab_background);
注意,mTabHost.newTabSpec("TAG1").setIndicator(yourTabItemview);這裡的"TAG1"其實沒什麼什麼意思,區分一下每個tab就好。
重點在於setIndicator函數,其有三個不同的實現,也就是說,你可以使用三種方式來定義你的Tab的風格:
[java] view plaincopy
- //只使用要文字標識tab
- TabHost.TabSpec.setIndicator(CharSequence label)
- //使用文字+icon標識tab
- TabHost.TabSpec.setIndicator(CharSequence label, Drawable icon)
- //使用自訂的View表示tab
- TabHost.TabSpec.setIndicator(View view)
前面兩種tab風格,是我們在絕大多數tabhost的範例中看到的風格(Holo風格),也就是當前選擇的tab下面會有類似於捲軸的一個高亮顯示的一個線條(indicator),很多時候我們不需要它,比如風格的Tab。這時候你就可以使用第三種方式來自訂你的Tab風格,你可以實現任何樣式的Tab:
[java] view plaincopy
- View yourTabItemView = layoutInflater.inflate(R.layout.tab_item_view, null);
-
- ImageView imageView = (ImageView) view.findViewById(R.id.imageview);
- imageView.setImageResource(mImageViewArray[index]);
-
- TextView textView = (TextView) view.findViewById(R.id.textview);
- textView.setText(mTextviewArray[index]);
另外,fragmentPage1.class是一個繼承自Fragment的類,在切換Tab時,會被動態執行個體化,並且add到R.id.realtabcontent這個內容容器中顯示
完成上面幾點,一個簡單的FragementActivity+FragmentTabHost+Fragment效果就出來了,接下來講如何調整Tab停靠在頂部還是底部。
當R.id.realtabcontent與R.id.tabhost不在一個布局檔案時,預設Tab在上TabContent在下,不能調整TabContent與Tab。
當R.id.realtabcontent與R.id.tabhost在一個布局檔案時,如果R.id.realtabcontent在R.id.tabhost上面,那麼Tab將會在TabContent下面,也就是說R.id.realtabcontent與R.id.tabhost的相對位置決定了選頁在上還是在下。
不要在布局檔案中給FragmentTabHost 設定子View,否則子View將顯示在以Tab左上方為座標0點的View中。給R.id.realtabcontent設定 android:layout_weight="1",因為預設時Tabcontent高度是wrap_content並且不能被調整,當Tab在Tabcontent下面並且顯示的View不足把Tab擠到底部時,Tab會掛在顯示的View的末尾,設定後Tabcontent就會被填充滿了。
源碼:http://download.csdn.net/detail/olevin/7563137