TabHost简单使用案例

 Android 中的Tabhost控件是个挺好用的控件,像一些分模块展示的页面就可以用Tabhost。

Tabhost的主要是由TabSpac组成的选项卡集合。TabSpec主要有两个重要方法,看代码:

[java] view plaincopy
 
  1. /** 
  2.  * A tab has a tab indicator, content, and a tag that is used to keep 
  3.  * track of it.  This builder helps choose among these options. 
  4.  * 
  5.  * For the tab indicator, your choices are: 
  6.  * 1) set a label 
  7.  * 2) set a label and an icon 
  8.  * 
  9.  * For the tab content, your choices are: 
  10.  * 1) the id of a {@link View} 
  11.  * 2) a {@link TabContentFactory} that creates the {@link View} content. 
  12.  * 3) an {@link Intent} that launches an {@link android.app.Activity}. 
  13.  */  
  14. public class TabSpec {  
  15.   
  16.     private String mTag;  
  17.   
  18.     private IndicatorStrategy mIndicatorStrategy;  
  19.     private ContentStrategy mContentStrategy;  
  20.   
  21.     private TabSpec(String tag) {  
  22.         mTag = tag;  
  23.     }  
  24.   
  25.     /** 
  26.      * Specify a label as the tab indicator. 
  27.      */  
  28.     public TabSpec setIndicator(CharSequence label) {  
  29.         mIndicatorStrategy = new LabelIndicatorStrategy(label);  
  30.         return this;  
  31.     }  
  32.   
  33.     /** 
  34.      * Specify a label and icon as the tab indicator. 
  35.      */  
  36.     public TabSpec setIndicator(CharSequence label, Drawable icon) {  
  37.         mIndicatorStrategy = new LabelAndIconIndicatorStrategy(label, icon);  
  38.         return this;  
  39.     }  
  40.   
  41.     /** 
  42.      * Specify a view as the tab indicator. 
  43.      */  
  44.     public TabSpec setIndicator(View view) {  
  45.         mIndicatorStrategy = new ViewIndicatorStrategy(view);  
  46.         return this;  
  47.     }  
  48.   
  49.     /** 
  50.      * Specify the id of the view that should be used as the content 
  51.      * of the tab. 
  52.      */  
  53.     public TabSpec setContent(int viewId) {  
  54.         mContentStrategy = new ViewIdContentStrategy(viewId);  
  55.         return this;  
  56.     }  
  57.   
  58.     /** 
  59.      * Specify a {@link android.widget.TabHost.TabContentFactory} to use to 
  60.      * create the content of the tab. 
  61.      */  
  62.     public TabSpec setContent(TabContentFactory contentFactory) {  
  63.         mContentStrategy = new FactoryContentStrategy(mTag, contentFactory);  
  64.         return this;  
  65.     }  
  66.   
  67.     /** 
  68.      * Specify an intent to use to launch an activity as the tab content. 
  69.      */  
  70.     public TabSpec setContent(Intent intent) {  
  71.         mContentStrategy = new IntentContentStrategy(mTag, intent);  
  72.         return this;  
  73.     }  


setIndicator()可以设置选项卡得图标和文字。

需要注意几点是: 1、如果你的Tabhost是从xml文件中findViewById()得到的,

                            TabWidget 必须为 android:id="@android:id/tabs" ,

                             FrameLayout android:id="@android:id/tabcontent" ;

            

[html] view plaincopy
 
  1. <TabHost android:id="@+id/tabhost_info" android:layout_width="fill_parent"  
  2.     android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android">  
  3.   
  4.     <LinearLayout android:id="@+id/linearLayout"  
  5.         android:layout_width="fill_parent" android:layout_height="fill_parent"  
  6.         android:orientation="vertical">  
  7.         <TabWidget android:id="@android:id/tabs"  
  8.             android:layout_width="fill_parent" android:layout_height="wrap_content">  
  9.         </TabWidget>  
  10.         <FrameLayout android:id="@android:id/tabcontent"  
  11.             android:layout_width="fill_parent" android:layout_height="wrap_content"  
  12.             android:layout_gravity="fill">  
  13.             <include android:id="@+id/info_include01" layout="@layout/info_layout01" />  
  14.             <include android:id="@+id/info_include02" layout="@layout/info_layout02" />  
  15.             <include android:id="@+id/info_include03" layout="@layout/info_layout03" />  
  16.   
  17.         </FrameLayout>  
  18.     </LinearLayout>  
  19.   
  20. </TabHost>  


              2、代码中,在添加TabWidget前,需要调用setup()方法。

              

[java] view plaincopy
 
  1.     tabHost=(TabHost)findViewById(R.id.tabhost_info);  
  2. tabHost.setup();   
  3. tabHost.addTab(tabHost.newTabSpec("信息")  
  4.              .setContent(R.id.info_include01)  
  5.              .setIndicator("基本信息",getResources().getDrawable(R.drawable.ic_launcher))  
  6.                       
  7. );  
  8. tabHost.addTab(tabHost.newTabSpec("更多信息")  
  9.          .setContent(R.id.info_include02)  
  10.          .setIndicator("更多信息",getResources().getDrawable(R.drawable.ic_launcher))  
  11.                   
  12.   
  13. tabHost.addTab(tabHost.newTabSpec("附件下载")  
  14.          .setContent(R.id.info_include03)  
  15.          .setIndicator("附件下载",getResources().getDrawable(R.drawable.ic_launcher))  
  16.                   

下面是自己写的一个demo:

          

posted @ 2015-05-17 06:46  壮汉请留步  阅读(448)  评论(0编辑  收藏  举报