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

自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. //为每个Tabbutton设置图标、文字和内容  
  2. TabSpec tabSpec = mTabHost.newTabSpec("TAG1").setIndicator(yourTabItemView);   
  3. //将Tabbutton加入进Tab选项卡中   
  4. mTabHost.addTab(tabSpec, fragmentPage1.classnull);   
  5. //设置Tabbutton的背景   
  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

posted @ 2017-06-11 11:19  jhcelue  阅读(1248)  评论(0编辑  收藏  举报