FragmentTabHost实现Fragment的tab效果

TabHost效果

 

[java] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. public class MainActivity extends FragmentActivity{   
  2.     //定义FragmentTabHost对象  
  3.     private FragmentTabHost mTabHost;  
  4.     //定义一个布局  
  5.     private LayoutInflater layoutInflater;  
  6.     //定义数组来存放Fragment界面  
  7.     @SuppressWarnings("rawtypes")  
  8.     private Class fragmentArray[] = {InspectFragment.class,ExecuteFragment.class,TaskFragment.class,ExitFragment.class};  
  9.     //定义数组来存放按钮图片  
  10.     private int mImageViewArray[] = {R.drawable.tab_more_btn,R.drawable.tab_home_btn,R.drawable.tab_selfinfo_btn,R.drawable.tab_message_btn};  
  11.       
  12.     public void onCreate(Bundle savedInstanceState) {  
  13.         this.requestWindowFeature(Window.FEATURE_NO_TITLE);  
  14.         super.onCreate(savedInstanceState);  
  15.         setContentView(R.layout.main_tab_layout);  
  16.         initView();  
  17.     }  
  18.   
  19.     /** 
  20.      * 初始化组件 
  21.      */  
  22.     private void initView(){  
  23.         //实例化布局对象  
  24.         layoutInflater = LayoutInflater.from(this);  
  25.   
  26.         //实例化TabHost对象,得到TabHost  
  27.         mTabHost = (FragmentTabHost)findViewById(android.R.id.tabhost);  
  28.         mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);   
  29.   
  30.         //得到fragment的个数  
  31.         int count = fragmentArray.length;     
  32.   
  33.         for(int i = 0; i < count; i++){   
  34.             //为每一个Tab按钮设置图标、文字和内容  
  35.             TabSpec tabSpec = mTabHost.newTabSpec(i+"").setIndicator(getTabItemView(i));  
  36.             //将Tab按钮添加进Tab选项卡中  
  37.             mTabHost.addTab(tabSpec, fragmentArray[i], null);  
  38.         }  
  39.     }  
  40.   
  41.     /** 
  42.      * 给Tab按钮设置图标和文字 
  43.      */  
  44.     private View getTabItemView(int index){  
  45.         View view = layoutInflater.inflate(R.layout.main_tab_item, null);  
  46.         TextView tab_item_view = (TextView) view.findViewById(R.id.tab_item_view);  
  47.         tab_item_view.setBackgroundResource(mImageViewArray[index]);  
  48.         return view;  
  49.     }  
  50. }  



 

注意在Fragment导包的时候要导import Android.support.v4.app.Fragment;

 

图标用背景选择器:

 

[html] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. <selector xmlns:android="http://schemas.android.com/apk/res/android">  
  2.   
  3.     <item android:drawable="@drawable/xpd" android:state_selected="true"/>  
  4.     <item android:drawable="@drawable/pd"/>  
  5.   
  6. </selector>  

 

布局文件

[html] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. <android.support.v4.app.FragmentTabHost  
  2.     android:id="@android:id/tabhost"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="wrap_content" >  
  5.   
  6.     <FrameLayout  
  7.         android:id="@android:id/tabcontent"  
  8.         android:layout_width="0dp"  
  9.         android:layout_height="50dp"  
  10.         android:layout_weight="0" />  
  11. </android.support.v4.app.FragmentTabHost>  
  12.   
  13. <FrameLayout  
  14.     android:id="@+id/realtabcontent"  
  15.     android:layout_width="fill_parent"  
  16.     android:layout_height="0dip"  
  17.     android:layout_weight="1" />  
posted @ 2016-12-30 14:58  天涯海角路  阅读(143)  评论(0)    收藏  举报