android底部选项卡(二)FragmentTabHost +Fragment 实现

前面一篇博客中提到, 使用 TabHost实现底部选项栏,但是TabHost已经过时点击打开链接

TabHost实现底部选项栏切换的的是Activity,而FragmentTabHost 切换的则是Fragment,相比之下更加灵活,性能更加优越

FragmentTabHost +Fragment 不仅能在不同选项卡间快速切换,而且还能保存其它选项卡之前的状态

1、FragmentTabHost 的Xml文件定义如下:

需要注意的是:FragmentTabHost 的id必须指定为系统的“@android:id/tabhost”

FragmentTabHost 上面的FrameLayout布局是用来填充对应Fragment的容器

 

[html] view plaincopy在CODE上查看代码片派生到我的代码片
 
  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.     <FrameLayout  
  7.         android:id="@+id/realtabcontent"  
  8.         android:layout_width="fill_parent"  
  9.         android:layout_height="0dip"  
  10.         android:layout_weight="1" />  
  11.   
  12.     <android.support.v4.app.FragmentTabHost  
  13.         android:id="@android:id/tabhost"  
  14.         android:layout_width="fill_parent"  
  15.         android:layout_height="wrap_content" >  
  16.         <FrameLayout  
  17.             android:id="@android:id/tabcontent"  
  18.             android:layout_width="0dp"  
  19.             android:layout_height="0dp"  
  20.             android:layout_weight="0" />              
  21.     </android.support.v4.app.FragmentTabHost>  
  22. </LinearLayout>  

 

 

2.java逻辑

 

[java] view plaincopy在CODE上查看代码片派生到我的代码片
 
  1. package com.example.fragmenttabhost;  
  2.   
  3. import android.os.Bundle;  
  4. import android.app.Activity;  
  5. import android.app.Fragment;  
  6. import android.support.v4.app.FragmentActivity;  
  7. import android.support.v4.app.FragmentTabHost;  
  8. import android.view.LayoutInflater;  
  9. import android.view.Menu;  
  10. import android.view.View;  
  11. import android.widget.ImageView;  
  12. import android.widget.TabHost.TabSpec;  
  13. import android.widget.TextView;  
  14.   
  15. public class MainActivity extends FragmentActivity {  
  16.     private LayoutInflater inflater;  
  17.     private FragmentTabHost mTabHost;  
  18.   
  19.     @Override  
  20.     protected void onCreate(Bundle savedInstanceState) {  
  21.         super.onCreate(savedInstanceState);  
  22.         setContentView(R.layout.activity_main);  
  23.         inflater=LayoutInflater.from(this);  
  24.   
  25.         <strong>mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);  
  26.         mTabHost.setup(getApplicationContext(), getSupportFragmentManager(),  
  27.                 R.id.realtabcontent);</strong>  
  28.   
  29.         // 添加tab名称和图标  
  30.         View indicator = getIndicatorView("我的联系人", R.drawable.tab_main_nav_me);  
  31.         TabSpec firstSpec = mTabHost.newTabSpec("myContact").setIndicator(  
  32.                 indicator);  
  33.         mTabHost.addTab(firstSpec, FirstFragment.class, null);  
  34.   
  35.         // 添加tab名称和图标  
  36.         View indicator2 = getIndicatorView("我的读书", R.drawable.tab_main_nav_book);  
  37.         TabSpec secondSpec = mTabHost.newTabSpec("myRead").setIndicator(  
  38.                 indicator2);  
  39.         mTabHost.addTab(secondSpec, SecondFragment.class, null);  
  40.     }  
  41.   
  42.     /** 
  43.      * 设置底部选项卡 
  44.      */  
  45.     private View getIndicatorView(String name, int iconid) {  
  46.         View view = inflater.inflate(R.layout.nav_tab, null);  
  47.         ImageView ivicon = (ImageView) view.findViewById(R.id.ivIcon);  
  48.         TextView tvtitle = (TextView) view.findViewById(R.id.tvTitle);  
  49.         ivicon.setImageResource(iconid);  
  50.         tvtitle.setText(name);  
  51.         return view;  
  52.     }  
  53. }  


源码下载地址http://download.csdn.net/detail/lang791534167/6837533

 

版权声明:本文为博主原创文章,未经博主允许不得转载。

posted on 2015-10-18 17:04  杭州糊涂虫  阅读(181)  评论(0)    收藏  举报

导航