FragmentTabHost的使用

首先创建一个XML布局,这个布局中包含TabHost的基本控件,你可以在FragmentTabHost标签上下增加些自己需要的控件。之后创建一个类继承FragmentActivity类,布局就是下面的XML。

[html]  view plain  copy
  1. <android.support.v4.app.FragmentTabHost  
  2.     android:id="@android:id/tabhost"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent" >  
  5.   
  6.     <LinearLayout  
  7.         android:layout_width="match_parent"  
  8.         android:layout_height="match_parent"  
  9.         android:orientation="vertical" >  
  10.   
  11.         <TabWidget  
  12.             android:id="@android:id/tabs"  
  13.             android:layout_width="match_parent"  
  14.             android:layout_height="wrap_content"  
  15.             android:layout_weight="0"  
  16.             android:orientation="horizontal" />  
  17.   
  18.         <FrameLayout  
  19.             android:id="@android:id/tabcontent"  
  20.             android:layout_width="0dp"  
  21.             android:layout_height="0dp"  
  22.             android:layout_weight="0" />  
  23.   
  24.         <FrameLayout  
  25.             android:id="@+id/realtabcontent"  
  26.             android:layout_width="match_parent"  
  27.             android:layout_height="0dp"  
  28.             android:layout_weight="1" />  
  29.     </LinearLayout>  
  30. </android.support.v4.app.FragmentTabHost>  

 创建一个类继承Fragment类,这个类用于FragmentTabHost的内容展示,这里只是简单的TextView显示
[java]  view plain  copy
  1. @Override  
  2. public View onCreateView(LayoutInflater inflater, ViewGroup container,  
  3.         Bundle savedInstanceState) {  
  4.     textView=new TextView(getActivity());  
  5.     textView.setText("FragmentStackSupport");  
  6.     return textView;  
  7. }  
  8.   
  9. @Override  
  10. public void onDestroyView() {  
  11.     super.onDestroyView();  
  12.     textView = null;  
  13. }  

下面是添加其他的Fragment到FragmentManager下管理。
[java]  view plain  copy
  1. mTabHost = (FragmentTabHost)findViewById(android.R.id.tabhost);  
  2. mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);  
  3.   
  4. /*1.addTab的第二个参数只能放继承Fragment的类 
  5.  *2.setIndicator的参数可以放入一个LayoutInflater出的Viwe 
  6.  * */  
  7. mTabHost.addTab(mTabHost.newTabSpec("simple").setIndicator("Simple"),  
  8.         FragmentStackSupport.classnull);  
  9. mTabHost.addTab(mTabHost.newTabSpec("contacts").setIndicator("Contacts"),  
  10.         LoaderCursorSupport.classnull);  

源码下载地址: http://download.csdn.net/download/aaren_jiang/5297855

作者:黑卡米     原文地址: http://blog.csdn.net/aaren_jiang/article/details/8780401

你可能感兴趣的:(FragmentTabHost)