Android乐学成语之TabHost介绍及使用Fragment 替换 TabActivity

TabHost介绍
TabHost组件可以在界面中存放多个选项卡, 很多软件都使用了改组件进行设计;
1. TabHost常用组件
TabWidget : 该组件就是TabHost标签页中上部 或者 下部的按钮, 可以点击按钮切换选项卡;
TabSpec : 代表了选项卡界面, 添加一个TabSpec即可添加到TabHost中;
-- 创建选项卡 : newTabSpec(String tag), 创建一个选项卡;
-- 添加选项卡 : addTab(tabSpec);

 

TabHost的使用请看http://blog.csdn.net/harvic880925/article/details/17120325/

 

自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就会被填充满了。

posted on 2016-06-15 13:04  戒酒的李白  阅读(369)  评论(0编辑  收藏  举报

导航