FragmentActivity+FragmentTabHost+Fragement替代TabActibvity+TabHost+Activity

來源:互聯網
上載者:User

自Android3.2之後,TabActibvity被棄用(Deprecated),取而代之的是FragmentActivity,因為Fragment比Activiy更靈活,消耗的資源更小,完全能夠滿足TabActivity的效果,所以直接替代之。原來的TabActibvity+TabHost+Activity那套還可以用,不過強烈建議改用FragmentActivity+FragmentTabHost+Fragement

FragmentTabHost用法:

1. 定義FragmentActivity的layout:

[html] view plaincopy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:orientation="vertical" >  
  6.   
  7.   
  8.     <FrameLayout  
  9.         android:id="@+id/realtabcontent"  
  10.         android:layout_width="fill_parent"  
  11.         android:layout_height="0dip"  
  12.         android:layout_weight="1" />  
  13.   
  14.   
  15.     <android.support.v4.app.FragmentTabHost  
  16.         android:id="@android:id/tabhost"  
  17.         android:layout_width="fill_parent"  
  18.         android:layout_height="wrap_content"   
  19.         android:background="@drawable/maintab_toolbar_bg">  
  20.   
  21.   
  22.         <FrameLayout  
  23.             android:id="@android:id/tabcontent"  
  24.             android:layout_width="0dp"  
  25.             android:layout_height="0dp"  
  26.             android:layout_weight="0" />              
  27.     </android.support.v4.app.FragmentTabHost>  
  28.   
  29.   
  30. </LinearLayout>  
2. 必須繼承FragmentActivity
[java] view plaincopy
  1. public class MainTabActivity extends FragmentActivity{    
  2.     //定義FragmentTabHost對象  
  3.     private FragmentTabHost mTabHost;  

3. 得到FragmentTabHost對象

[java] view plaincopy
  1. mTabHost = (FragmentTabHost)findViewById(android.R.id.tabhost);  

4. 初始化FragmentTabHost對象

[java] view plaincopy
  1. mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);   

注意,這裡的R.id.realtabcontent可以是任一個ViewGroup或其子類的對象id,比如LinearLayout。其實際作用就是個容器,Tab切換時,當前Tab對應的Fragment會被加入到這個ViewGroup作為其子View

5.按順序添加每個Tab頁

[java] view plaincopy
  1. //為每一個Tab按鈕設定表徵圖、文字和內容  
  2. TabSpec tabSpec = mTabHost.newTabSpec("TAG1").setIndicator(yourTabItemView);   
  3. //將Tab按鈕添加進Tab選項卡中   
  4. mTabHost.addTab(tabSpec, fragmentPage1.class, null);   
  5. //設定Tab按鈕的背景   
  6. mTabHost.getTabWidget().getChildAt(i).setBackgroundResource(R.drawable.selector_tab_background);  

注意,mTabHost.newTabSpec("TAG1").setIndicator(yourTabItemview);這裡的"TAG1"其實沒什麼什麼意思,區分一下每個tab就好。

重點在於setIndicator函數,其有三個不同的實現,也就是說,你可以使用三種方式來定義你的Tab的風格:
[java] view plaincopy
  1. //只使用要文字標識tab   
  2. TabHost.TabSpec.setIndicator(CharSequence label)    
  3. //使用文字+icon標識tab   
  4. TabHost.TabSpec.setIndicator(CharSequence label, Drawable icon)   
  5. //使用自訂的View表示tab   
  6. TabHost.TabSpec.setIndicator(View view)  

前面兩種tab風格,是我們在絕大多數tabhost的範例中看到的風格(Holo風格),也就是當前選擇的tab下面會有類似於捲軸的一個高亮顯示的一個線條(indicator),很多時候我們不需要它,比如風格的Tab。這時候你就可以使用第三種方式來自訂你的Tab風格,你可以實現任何樣式的Tab:

[java] view plaincopy
  1. View yourTabItemView = layoutInflater.inflate(R.layout.tab_item_view, null);  
  2.   
  3. ImageView imageView = (ImageView) view.findViewById(R.id.imageview);  
  4. imageView.setImageResource(mImageViewArray[index]);  
  5.   
  6. TextView textView = (TextView) view.findViewById(R.id.textview);          
  7. 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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.