剖析下Android里面的Tab


今天研究了TabView但是发现一个奇怪的现象,从无数网上的代码来看,TabView的呈现都有相关的布局,好吧,先就布局来说,有2个样子,一个是TabView置顶,一个是至底,要说这2个有什么不同的话,其实也就布局的不同。先看看置顶的布局

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <TabHost xmlns:Android="http://schemas.android.com/apk/res/android"  
  3.     android:id="@android:id/tabhost"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent">  
  6.     <LinearLayout  
  7.         android:orientation="vertical"  
  8.         android:layout_width="fill_parent"  
  9.         android:layout_height="fill_parent">  
  10.         <TabWidget  
  11.             android:id="@android:id/tabs"  
  12.             android:layout_width="fill_parent"  
  13.             android:layout_height="wrap_content"/>  
  14.             <FrameLayout      
  15.                                              android:id="@android:id/tabcontent"  
  16.                 android:layout_width="fill_parent"  
  17.                 android:layout_height="fill_parent">  
  18.                    
  19.                    
  20.     </FrameLayout>  
  21.     </LinearLayout>  
  22. </TabHost>  

下面再看看置低的布局

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <TabHost xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:id="@android:id/tabhost"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent">  
  6.     <LinearLayout  
  7.         android:orientation="vertical"  
  8.         android:layout_width="fill_parent"  
  9.         android:layout_height="fill_parent">  
  10.            
  11.             <FrameLayout  
  12.                 android:id="@android:id/tabcontent"  
  13.                 android:layout_width="fill_parent"  
  14.                 android:layout_height="wrap_content">  
  15.                    
  16.                    
  17.             </FrameLayout>  
  18.             <TabWidget  
  19.             android:id="@android:id/tabs"  
  20.             android:layout_width="fill_parent"  
  21.             android:layout_height="wrap_content"/>  
  22.     </LinearLayout>  
  23. </TabHost>  

仔细研究下就可以看出来区别了仅仅是<TabWidget>和<FrameLayout>的位置不同而已。问题似乎就此也就结束了,然而,你在android SDk提供的相关列子APIdemos你会发现其源码里面根本就没有所谓的什么<TabHost><TabWidget>。这是什么原因?而且它也可以正常的运行。我先贴一个代码,你完全可以自己试试看,是不是用不着布局呢?

  1. public class Tabs3 extends TabActivity {   
  2.     @Override  
  3.     protected void onCreate(Bundle savedInstanceState) {   
  4.         super.onCreate(savedInstanceState);   
  5.         final TabHost tabHost = getTabHost();   
  6.         tabHost.addTab(tabHost.newTabSpec("tab1")   
  7.                 .setIndicator("list")   
  8.                 .setContent(new Intent(this, List1.class)));   
  9.         tabHost.addTab(tabHost.newTabSpec("tab2")   
  10.                 .setIndicator("photo list")   
  11.                 .setContent(new Intent(this, List8.class)));   
  12.            
  13.         // This tab sets the intent flag so that it is recreated each time   
  14.         // the tab is clicked.   
  15.         tabHost.addTab(tabHost.newTabSpec("tab3")   
  16.                 .setIndicator("destroy")   
  17.                 .setContent(new Intent(this, Controls2.class)   
  18.                         .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)));   
  19.     }   
  20. }  

当然这里面用到了Intent的跳转,似乎也就用不是布局了,这不是重点。重点是所谓的TabHost为什么不需要?目前作者还没想明白,有精通的大哥能否告诉我呢。。。

最后再说一件有意思的事情,目前我看到的有这种Tab效果的已经发现有好几个不是用的TabHost来做的,当然,玩转四方还是用的这个。

相关内容